1

The problem I have is that when more rows are added to JTable (jtbls) the vertical scrollbar doesn't appear on my JScrollPane (outer).

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 4));
JScrollPane outer = new JScrollPane(panel);

jtbls = new JTable[4];
for (int i = 0; i < jtbls.length; i++) {
    jtbls[i] = new JTable(new MyTableModel());
    jtbls[i].setFillsViewportHeight(true);
    jtbls[i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));

    JPanel inner = new JPanel(new BorderLayout());
    inner.add(jtbls[i], BorderLayout.CENTER);
    inner.add(jtbls[i].getTableHeader(), BorderLayout.NORTH);
    inner.setPreferredSize(new Dimension(outer.getWidth() / 4, 70));

    panel.add(inner);
}

add(outer);

Screenshot

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
abg
  • 2,002
  • 7
  • 39
  • 63

2 Answers2

2

You can set all but one of the scroll bars to VERTICAL_SCROLLBAR_NEVER and set one to VERTICAL_SCROLLBAR_ALWAYS. Then tie the scroll bar models together like @camickr showed me here.

Community
  • 1
  • 1
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
1

This is because you set

inner.setPreferredSize(new Dimension(outer.getWidth() / 4, 70));

So inner JPanel is "fine" with height above 70 and don't requests more space to the parent - JPanel panel which won't request more space to its parent - JVieport.

Also if you look at the source code of JTable - method setFillsViewportHeight is only applicable to the javax.swing.JLayer, javax.swing.ScrollPaneLayout, javax.swing.ViewportLayout (look at invocations of getScrollableTracksViewportHeight()) - so this will work only when JTable is inside some viewport.

Xeon
  • 5,949
  • 5
  • 31
  • 52