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
  • 50,242
  • 28
  • 96
  • 201
luca
  • 11,661
  • 14
  • 65
  • 102
  • 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

7

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
  • 657,433
  • 87
  • 1,179
  • 1,226