0

I want to make the table width at least fitting the enclosing scroll pane's width, and if it's getting larger, the horizontal scroll bar is used.

None of JTable's auto resize modes suits my need: AUTO_RESIZE_OFF does allow the table to be resized larger than the scroll pane's width, but it can't limit the table's minimal width; the other modes don't allow the table width to be larger than the scroll pane's width at all.

I have tried to set the minimal size as below but it didn't work:

JTable table = new JTable(myModel);
JScrollPane scrollPane = new JScrollPane(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

// After the table is displayed.
table.setMinimalSize(scrollPane.getSize());

Any idea to achieve that?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Zhao Yi
  • 2,335
  • 7
  • 23
  • 27
  • Take a look at [this example](http://stackoverflow.com/questions/15014950/jtable-horizontal-scrollbar-based-on-width-of-one-column/15015445#15015445) – MadProgrammer Jul 10 '13 at 05:57
  • 2
    Try `setPreferredSize()` in addition to `setMinimalSize()` most layout managers honor the preferred size rather than the minimum size. –  Jul 10 '13 at 05:59
  • @a_horse_with_no_name: If I use `setPreferredSize()`, the table width will become fixed to the preferred width, and can never be resized again. – Zhao Yi Jul 10 '13 at 06:34
  • @MadProgrammer: Seems that's a different question... – Zhao Yi Jul 10 '13 at 06:41
  • @ZhaoYi Really? How? It will cause the columns to fill the horizontal width of the scroll pane when the total width of the table is smaller then the viewport, but will allow it to over expand (showing the horizontal scroll bars when it's to large) – MadProgrammer Jul 10 '13 at 06:43
  • Not in my experience. The width is limited by setMaximumWidth() not by the preferred width. But this also depends on the layout manager. –  Jul 10 '13 at 06:50
  • @MadProgrammer: Just tested your code, and when I tried to resize a column by dragging the separator, a `StackOverFlowException` was thrown. – Zhao Yi Jul 10 '13 at 06:56
  • @ZhaoYi I did say it was hack. I've made some bug fixes, should be working, better :P – MadProgrammer Jul 10 '13 at 07:18
  • @MadProgrammer: Can you post your newest code? – Zhao Yi Jul 10 '13 at 07:25
  • @ZhaoYi Yeah, I updated the linked question – MadProgrammer Jul 10 '13 at 07:28
  • @MadProgrammer: No exceptions are thrown this time but the table width can't be resized (by dragging the last separator in the header) to make the horizontal scroll bar appear. – Zhao Yi Jul 10 '13 at 07:40
  • @ZhaoYi So, as I understand it, you require it that the table be sized to fill the viewport when the table size is smaller the then viewport, but if the viewport is smaller the then the table, it will show the horizontal scrollbar? – MadProgrammer Jul 10 '13 at 07:44
  • @MadProgrammer: Correct! In another word, the table width should never be smaller than the viewport width. – Zhao Yi Jul 10 '13 at 08:15

1 Answers1

3

I have tried to set the minimal size as below but it didn't work:

table.setMinimalSize(scrollPane.getSize());

  • JTable and JScrollPane can't returns reasonable Min, Max and PreferredSize

  • you can to fit (on applications startup) JScrollPanes dimension to JTable by using table.setPreferredScrollableViewportSize(table.getPreferredSize()); (carefully with number of row, otherwise you have to calculating a Dimension instead of JTable.getP...) for JFrame.pack(), on runtime must be validated by revalidate() and repaint() for container nested JScrollPane, don't do that

  • don't reinvent the wheel, to use Fixed Column Table of Table Column Adjuster by @camickr, which one isn't clear from your description, depends of your ...

a JTable's width >= the enclosing scroll pane's width?

  • couldn't be reversed this idea
mKorbel
  • 109,525
  • 20
  • 134
  • 319