Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable in a JScrollPane : no scrollbars!

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
Eelco
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS for the
table you describe.
HTH,
Joe
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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?
 
I do some of my very best work in water. Like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic