2

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);
kleopatra
  • 51,061
  • 28
  • 99
  • 211
luca
  • 12,311
  • 15
  • 70
  • 103
  • Basically, you have to learn the exact behaviour of LayoutManagers. Then _always_ use a LayoutManager (your right panel has none). _Never-ever_ use any of the setXXSize methods (for reasons, see http://stackoverflow.com/questions/7229226/avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swing) – kleopatra Oct 13 '11 at 06:40

1 Answers1

9

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255