user3212493
user3212493

Reputation: 165

How to dynamically add components to Java JScrollPane

I am trying to make a GUI application in Java but I am having trouble in adding/updating components dynamically in JScrollPane. I have two JPanels (P1 and P2) in which P1 has a form to set parameters for application and the P2 contains some GUI components, which are updated dynamically based on the values in the P1. I need a JScrollPane on P2 to scroll, so I added JScrollPane in P2. I added both P1 and P2 to a main panel "main" and then added the main panel to a frame. But the components are not updated in P2. Can someone suggest what is the problem? I have called revalidate(), repaint() and some other methods but GUI is not updated. Below is a sample code I have written just to illustrate my problem. I need GroupLayout in my application so here I also used GroupLayout

        import java.awt.event.ActionEvent;
        import javax.swing.GroupLayout;
        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.JPanel;
        import javax.swing.JScrollPane;


        public class JframeExample extends JFrame {

            private final JPanel P1;
            private final JPanel P2;
            private final JPanel main;
            private final JScrollPane scrol;
            private final JButton jButton;
            private final JButton jButton2;

            public JframeExample() {
                P1 = new JPanel();
                P2 = new JPanel();
                main = new JPanel();
                jButton = new JButton("Add");
                jButton2 = new JButton("Remove");
                scrol = new JScrollPane();
                initialize();
                this.add(main);
                this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                this.setSize(400, 400);
                this.setVisible(true);

            }

            public static void main(String[] args) {
                JframeExample jframeExample = new JframeExample();

            }

            private void addPressed(ActionEvent evt) {
                System.out.println("Add Pressed");
                scrol.add(new JButton());
                revalidate();
            }

            private void removePressed(ActionEvent evt) {
                System.out.println("Remove Pressed");
                scrol.removeAll();
                revalidate();
            }

            private void initialize() {
                GroupLayout layout = new GroupLayout(P1);
                P1.setLayout(layout);
                layout.setAutoCreateGaps(true);
                layout.setAutoCreateContainerGaps(true);
                GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
                hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton).addComponent(jButton2));

                layout.setHorizontalGroup(hGroup);
                GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
                vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jButton))
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jButton2));
                layout.setVerticalGroup(vGroup);
                P2.add(scrol);
                jButton.addActionListener((ActionEvent evt) -> {
                    addPressed(evt);
                });
                jButton2.addActionListener((ActionEvent evt) -> {
                    removePressed(evt);
                });
                GroupLayout layoutMain = new GroupLayout(main);
                main.setLayout(layoutMain);
                layoutMain.setAutoCreateGaps(true);
                layoutMain.setAutoCreateContainerGaps(true);
                layoutMain.setHorizontalGroup(layoutMain.createSequentialGroup()
                        .addComponent(P1).addComponent(P2));
                layoutMain.setVerticalGroup(layoutMain
                        .createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(P1)
                        .addComponent(P2));

            }

        }

Upvotes: 4

Views: 2595

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

Wrapping P2 inside JScrollPane also does not work.

Yes it does, because that's the way it work. If you take the time to read through the How to use scroll panes, examine the examples and maybe even consult the JavaDocs it would provide you with the basic information you'd need to get you UI up and running.

enter image description here

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class JframeExample extends JFrame {

    private final JPanel P1;
    private final JPanel P2;
    private final JPanel main;
    private final JScrollPane scrol;
    private final JButton jButton;
    private final JButton jButton2;

    public JframeExample() {
        P1 = new JPanel();
        P2 = new JPanel();
        main = new JPanel();
        jButton = new JButton("Add");
        jButton2 = new JButton("Remove");
        scrol = new JScrollPane(P2);
        initialize();
        this.add(main);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        this.setSize(400, 400);
        this.setVisible(true);

    }

    public static void main(String[] args) {
        JframeExample jframeExample = new JframeExample();

    }

    private void addPressed(ActionEvent evt) {
        System.out.println("Add Pressed");
        P2.add(new JButton());
        revalidate();
    }

    private void removePressed(ActionEvent evt) {
        System.out.println("Remove Pressed");
        P2.removeAll();
        revalidate();
    }

    private void initialize() {
        main.setLayout(new GridLayout(1, 2));
        main.add(P1);
        main.add(scrol);
        jButton.addActionListener((ActionEvent evt) -> {
            addPressed(evt);
        });
        jButton2.addActionListener((ActionEvent evt) -> {
            removePressed(evt);
        });
        P1.add(jButton);
        P1.add(jButton2);
    }

}

Word of warning GroupLayout really isn't meant for hand coding, it's really designed for UI editors.

Upvotes: 5

Related Questions