Question

I am posting a short example code and 2 screen shots. The first one does not show the panels inside the mainPanel (which is inside the frame) and I thought they are just not showing up.

When I expanded the frame width-wise just a little bit, the panels showed up, despite of the fact that the sizes of the panels have not been set explicitly. What is the reason behind it and how can I avoid this in my serious application?

enter image description here

enter image description here

public class test extends JFrame {
    public test() {
    }
    public static void main(String [] args){
        JFrame frame= new JFrame();
        Dimension size= frame.getPreferredSize();
        size.width=200;
        size.height=300;
        frame.setPreferredSize(size);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true); 

        frame.getContentPane().setLayout(new BorderLayout()); 

        JPanel mainPanel= new JPanel();
        mainPanel.setBorder(BorderFactory.createTitledBorder(
                new EtchedBorder(), "Details", TitledBorder.CENTER,
                TitledBorder.DEFAULT_POSITION, new Font("calibri", Font.PLAIN,
                        10)));
        frame.getContentPane().add(mainPanel, BorderLayout.CENTER);

        mainPanel.setLayout(new GridBagLayout()); 

        JPanel panel1= new JPanel();
        panel1.setBorder(BorderFactory.createTitledBorder(
                new EtchedBorder(), "Panel1", TitledBorder.CENTER,
                TitledBorder.DEFAULT_POSITION, new Font("calibri", Font.PLAIN,
                        10)));
        //check...panel1.setBackground(Color.black);
        GridBagConstraints c= new GridBagConstraints();
        c.gridx= 0;
        c.gridy=0;
        c.weightx=1;
        c.weighty=1;
        c.fill= c.BOTH;
        mainPanel.add(panel1, c); 

        JPanel panel2= new JPanel();
        panel2.setBorder(BorderFactory.createTitledBorder(
                new EtchedBorder(), "Panel2", TitledBorder.CENTER,
                TitledBorder.DEFAULT_POSITION, new Font("calibri", Font.PLAIN,
                        10)));
        //check..panel2.setBackground(Color.red); 
        c= new GridBagConstraints();
        c.gridx=0;
        c.gridy=1;
        c.weightx=1;
        c.weighty=2;
        c.fill= c.BOTH;
        mainPanel.add(panel2, c);

        JPanel panel3= new JPanel();
        panel3.setBorder(BorderFactory.createTitledBorder(
                new EtchedBorder(), "Panel3", TitledBorder.CENTER,
                TitledBorder.DEFAULT_POSITION, new Font("calibri", Font.PLAIN,
                        10)));
        //check...panel3.setBackground(Color.blue);
        c= new GridBagConstraints();
        c.gridx=0;
        c.gridy=2;
        c.weightx=1;
        c.weighty=6;
        c.fill=c.BOTH;
        mainPanel.add(panel3, c);

    }
}
Was it helpful?

Solution

frame.setVisible(true); 

The above statement must be executed AFTER all the components have been added to the frame and its child panels.

When I expanded the frame width-wise just a little bit, the panels showed up,

When the frame is resized, the layout manager is invoked so the components can be postioned and painted properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top