user2543653
user2543653

Reputation: 3

Container panels and JScrollPane

I'm working on GUI and I'm an amateur programmer. I have a problem with this code. I can't see anything on the frame.

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1366, 768);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

Container midPanel = new JPanel();
midPanel.setLayout(null);
Dimension preferredSize = new Dimension(700, 700);
midPanel.setPreferredSize(preferredSize);

            .....

Container k1 = new JPanel();
k1.setSize(50, 700);
k1.setLocation(0, 0);
k1.setLayout(new GridLayout(rowNum, 1));

k1.setVisible(true);

midPanel.add(k1);

            .......

Dimension jspD = new Dimension(500,500); 
JScrollPane jsp = new JScrollPane(midPanel);
jsp.setPreferredSize(jspD);
jsp.setLocation(0, 0);
jsp.setVisible(true);

contentPane.add(jsp);

I would appreciate your help.

Upvotes: 0

Views: 183

Answers (1)

Azad
Azad

Reputation: 5055

midPanel.setLayout(null);

You should always use Layout Managers, never ever remove the layout for any reason, except if you had an assignment required to use absolute layout (null layout).

The problem is with absolute layout , you have to specify the location of components inside the panel by component.setBounds(x,y,width,height) every time adding a component, otherwise, it won't be visible.

See this tutorial on Using Layout Managers.

Upvotes: 8

Related Questions