Search code examples
javaswinglayout-managergridbaglayout

Layout method breaks after first execution


When the addComponents() method is run once, I get the desired layout of my JPanel. However, when the code in addComponents() is executed more than once, the layout of the JPanel is completely wrong. Is there anything that I seem to be doing wrong?

public class DeleteStudent extends JPanel {

        public SearchPanel search = new SearchPanel();
        private final JButton deleteButton = new JButton("Delete from database");
        private GridBagConstraints cons = new GridBagConstraints();
        private final GridBagLayout gridBag = new GridBagLayout();

        public DeleteStudent() {
        super();
        setLayout(gridBag);
        setPreferredSize(new Dimension(400, 300));

        addComponents();
        addComponents(); //Method fails when run more than once!
        }

        public void addComponents() {
        cons.gridy = 1;
        cons.insets = new Insets(50, 0, 0, 0);
        gridBag.setConstraints(deleteButton, cons);

        removeAll();
        add(search);
        add(deleteButton);
        update();
        }

        private void update() {
        revalidate();
        repaint();
        }

Screenshots:

JPanel after 1 method call: http://img402.imageshack.us/img402/6409/oncer.png

JPanel after 2 method calls: http://imageshack.us/scaled/landing/254/twiced.png


Solution

  • Adding to my comment:

    Seems problem is you set the JPanels GridBagLayout constraints before calling removeAll() it should be done after calling removeAll(); so that when we add the new components the LayoutManager is still in effect and hasnt been reset to its defaults values.

    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Test {
    
        public Test() {
            createAndShowGui();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Test();
                }
            });
        }
    
        private void createAndShowGui() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new DeleteStudent());
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    class DeleteStudent extends JPanel {
    
        public JPanel search = new JPanel() {//for testing
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
        };
        private final JButton deleteButton = new JButton("Delete from database");
        private GridBagConstraints cons = new GridBagConstraints();
        private final GridBagLayout gridBag = new GridBagLayout();
    
        public DeleteStudent() {
            super();
            setLayout(gridBag);
    
            addComponents();
            addComponents(); //Method fails when run more than once!
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 300);
        }
    
        public void addComponents() {
            removeAll();//must call this before resetting Layout and adding new components
    
            cons.gridy = 1;
            cons.insets = new Insets(50, 0, 0, 0);
            gridBag.setConstraints(deleteButton, cons);
    
            add(search);
            add(deleteButton);
            update();
        }
    
        private void update() {
            revalidate();
            repaint();
        }
    }