Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable in a JScrollPane : no scrollbars!  RSS feed

Eelco den Heijer
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to build a JTable that has scrollbars when the data of the table does not fit in the visible part.
Here's what I do:
JTable table = new JTable(....);
JScrollPane scrollPane = new JScrollPane(table);
This does not provide any scrollbars. On the tutorial page of Sun/ Swing I read several tips, like:
contentPane.setPreferredSize(new Dimension(x,y));
This didn't work (my compiler complains that Container class does not know the setPreferredSize(...) method)
I tried other stuff: like:
table.setPreferredScrollableViewportSize(new Dimension(x,y));
no luck either.
Obviously, I'm totally missing the point on how to use the JScrollPane stuff.
In summary:
the JTable is about 800 pixels wide, and about 300 pixels high (depending on amount of rows visible).
The visible part of the table (the JScrollPane) is 550 pixels wide and 300 pixels high.
How to get scrollbars on the table?
Any pointers are more than welcome!
Thanks in advance,
Joe Gilvary
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a note in the tutorial about some versions
of Swing having problems with the scroll policies
X_AS_NEEDED. Their suggestions include updating
the version of Swing, if possible, or (in your
case) setting the preferred size on the JTable,
then calling revalidate on the JTable. (That is,
the size of the table itself, not the viewport.)
Another suggestion they offer is to be sure you
set the policy the way you want it.
You might get away with setting the horizontal
scrolling policy to
table you describe.
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theres a method in the JTable class:

Check it out in the API docs. I use tables a lot at work and when you construct a JScrollPane passing in an instance of a JTable it always shows the bars when needed. I have never had any problems with this. (I've had other more hairy problems )
What container are you putting the scrollpane in and what layout does it have? Are you adding the data after displaying the JTable? If so are you invoking the fireTableDataChanged() method on the TableModel?
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!