File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTable in a JScrollPane : no scrollbars! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable in a JScrollPane : no scrollbars!" Watch "JTable in a JScrollPane : no scrollbars!" New topic

JTable in a JScrollPane : no scrollbars!

Eelco den Heijer
Ranch Hand

Joined: Jan 17, 2002
Posts: 61
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,

== <br />Rgrds,<br />Eelco<br /> <br />SCJP, SCJD, SCBCD, SCWD, SCEA
Joe Gilvary
Ranch Hand

Joined: May 11, 2001
Posts: 152
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

Joined: May 30, 2001
Posts: 328
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?

Raising Flares debut album 'Ignition' out now

Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)
I agree. Here's the link:
subject: JTable in a JScrollPane : no scrollbars!
It's not a secret anymore!