Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JScrollPane inside BorderLayout.CENTER doesn't resize

This is the code below, the problem is that the JScrollPane stays at its preferred size and doesn't resize with the BorderLayout.CENTER region. I want it to fill that region, of course.. and use it to scroll the contents of the table inside it.

frame = new JFrame();
frame.setBounds(100, 100, 500, 400);
JPanel panelCenter = new JPanel();
FlowLayout flowLayout = (FlowLayout) panelCenter.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
panelCenter.setPreferredSize(new Dimension(300, 400));

panelRight = new ActorDrawer();
frame.getContentPane().add(panelRight, BorderLayout.EAST);
panelRight.setLayout(null);
panelRight.setPreferredSize(new Dimension(200, 400));
frame.getContentPane().add(panelCenter, BorderLayout.CENTER);

table = new JTable(new MyTableModel());
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
panelCenter.add(scrollPane);
like image 574
luca Avatar asked Aug 20 '26 17:08

luca


1 Answers

You don't add the JScrollPane to the center of the panel with the BorderLayout: you add it to another panel, with a FlowLayout, and this panel is added to the center. Remove the panelCenter.

like image 161
JB Nizet Avatar answered Aug 22 '26 07:08

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!