This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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
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?