1

I'm trying to create a JFrame with one main panel contains two panels: left sub-panel has add and remove buttons which will dynamically add and remove components; right panel contains regular component. My code works find when there is only one panel, fails when used inside sub-panel.

public class MultiPanel extends JFrame{

    static MultiPanel myFrame;
    static int countMe = 0;
    JPanel mainPanel;
    JPanel userPanel;
    JPanel contentPanel;

    private static void iniComponents() {
        myFrame = new MultiPanel();
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.prepareUI();
        myFrame.pack();
        myFrame.setVisible(true);
    }

    private void prepareUI() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("My Title");
        setPreferredSize(new java.awt.Dimension(1280, 720));
        setSize(new java.awt.Dimension(1280, 720));

        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));

        userPanel = new JPanel();
//      userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.Y_AXIS));

        mainPanel.add(userPanel);

        JButton buttonAdd = new JButton("Add subPanel");
        buttonAdd.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                userPanel.add(new subPanel());
                myFrame.pack();
            }
        });

        JButton buttonRemoveAll = new JButton("Remove All");
        buttonRemoveAll.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                userPanel.removeAll();
                myFrame.pack();
            }
        });

        contentPanel = new JPanel();
        JLabel jLabel1 = new JLabel("Content here");
        contentPanel.add(jLabel1);
        mainPanel.add(contentPanel);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel)
        );

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel)
        );

//      getContentPane().add(userPanel, BorderLayout.WEST);
//      getContentPane().add(contentPanel, BorderLayout.EAST);       
//      getContentPane().add(buttonAdd, BorderLayout.PAGE_START);
//      getContentPane().add(buttonRemoveAll, BorderLayout.PAGE_END);
    }

    private class subPanel extends JPanel {

        subPanel me;

        public subPanel() {
            super();
            me = this;
            JLabel myLabel = new JLabel("This is  subPanel(): " + countMe++);
            add(myLabel);
            JButton myButtonRemoveMe = new JButton("remove me");
            myButtonRemoveMe.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {
                    me.getParent().remove(me);
                    myFrame.pack();
                }
            });
            add(myButtonRemoveMe);
        }              
    }

    public static void main(String args[]) {

        SwingUtilities.invokeLater(() -> {
            iniComponents();
        });
    }
}

I also wonder what is the proper way to write about the layout, aligning and organizing components seems to be painful for me.

Daolin
  • 614
  • 1
  • 16
  • 41
  • 1
    *"left subpanel"? What not identify them by the names in code? `userPanel` is within the large panel, but very small and the same color, so invisible. `contentPanel` is never added to anything. – Andrew Thompson May 10 '16 at 05:26
  • *"I also wonder what is the proper way to write about the layout, aligning and organizing components seems to be painful for me."* Making a GUI work across different platforms and PLAFs is not an easy task. This is why we rely on layouts. Use [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). BTW - Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson May 10 '16 at 05:29
  • *"..has add and remove buttons which will dynamically add and remove components;"* Why? What is the ultimate purpose of this? The only thing I can think of, for which that is likely the *best approach,* is 'tool for programmers to make GUIs'.. – Andrew Thompson May 10 '16 at 05:33
  • I added the right panel, now it only shows right contentPanel. – Daolin May 10 '16 at 05:46
  • Add a 5 pixel (or so) colored border to each panel. A different color for each panel. – Andrew Thompson May 10 '16 at 06:14

0 Answers0