srk20
srk20

Reputation: 1

JPanel does not show up inside another JPanel

class ABC extends JFrame {
    public JPanel createGUI()
    {
        JPanel outerPanel = new JPanel();
        outerPanel.setLayout(null);

        JLabel top = new JLabel();
        top.setBounds(40,40,400,30);
        top.setText("Hello World");
        outerPanel.add(top);

        int l = getLength();
        JPanel innerPanel = new JPanel();
        if(l==0)
        {
            innerPanel.setLayout(null);
            JLabel empty = new JLabel("No Data Found");
            empty.setBounds(80,150,300,30);
            innerPanel.add(empty);
        }
        else
        {
            innerPanel.setLayout(new GridLayout(l,4,5,5)); 
            for(int i=0;i<l;i++)
            {
                innerPanel.add(new JLabel("Text1");
                innerPanel.add(new JLabel("Text2");

                JButton b1 = new JButton("Button1");
                innerPanel.add(b1);
                JButton b2 = new JButton("Button2");
                innerPanel.add(b2);
            }          
         }
         outerPanel.add(innerPanel);
         return outerPanel;
    }
}

In the above code the innerPanel does not show up and neither any error occurs.Any idea how to show up the innerPanel which is inside an outerPanel.I tried using getContentPane().add(innerPanel) but it didn't work.

Upvotes: 0

Views: 225

Answers (1)

Adam Saunders
Adam Saunders

Reputation: 380

Try changing

outerPanel.setLayout(null);

to

outerPanel.setLayout(new FlowLayout());

or remove the setLayout call entirely.

Upvotes: 1

Related Questions