How to dynamically add components to Java JScrollPane

后端 未结 1 724
说谎
说谎 2021-01-19 23:48

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 ha

相关标签:
1条回答
  • 2021-01-20 00:20

    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.

    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.

    0 讨论(0)
提交回复
热议问题