aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Height of JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Height of JTable" Watch "Height of JTable" New topic
Author

Height of JTable

Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Hi,
How to set the height of the JTable? I have several records but would like to display
only 10 records at a time on screen. User can view other records by scrolling.
Thanks
Scott Pedigo
Greenhorn

Joined: Feb 15, 2001
Posts: 15
Well, inherited from Component you have method setSize(int, int),
and if you want you can override getPreferredSize and/or getMinimumSize() which are inherited from Container. The latter will prevent the table being resized to something you don't want. None of these will give you ten rows - they'll just set the size of the whole table, and depending on the font size you'll get more or less rows.
However below is probably what you want.
I got this excerpt directly from the Javadocs and pasted it in
here. I suggest putting a bookmark in your browser to http://java.sun.com/j2se/1.3/docs/api/index.html
"The JTable has many facilities that make it possible to customize its rendering and editing but provides
defaults for these features so that simple tables can be set up easily. For example, to set up a table with 10
rows and 10 columns of numbers:

I also found this for JTable:
setPreferredScrollableViewportSize(Dimension size)

[This message has been edited by Scott Pedigo (edited February 16, 2001).]
Tim Kemper
Greenhorn

Joined: Jan 05, 1999
Posts: 4
I believe that the question deals with "displaying" a set number of rows in a table. The table model's getRowCount deals with the number of rows in the table (the table will only call getValueAt for the number of rows and cols returned by the getXXCount methods of the table model.
So we are really talking about sizing of the table. If your layout is done properly (not using setSize()) you should be able to set the preferred size of your table (actually the JScrollPane that the table is in) and it will show the number of desired rows (once pack() is called).
Now that that's out of the way here's how to do it:
1. Get the height of the rows - table.getRowHeight() * numOfRows
2. Get the intercell spacing - table.getIntercellSpacing().height * (numRows +1)
3. Get the height of the table header - table.getTableHeader().getPreferredSize().height
4. Add the preceeding together to get the needed table height
Since we need to set the size of the scroll pane ....
5. Add the scrollPane insets - Insets insets = scrollPane.getInsets();
scrollInsetHeight = insets.top + insets.bottom;
6. add scrollInsetHeight to tableHeight and set to scrollPane preferred size, ie:
7. scrollPane.setPreferredSize(new Dimension(scrollPane.getPreferredSize().width, newHeight));
This method is especially useful when you have the ability to change the font size in your application. This allows the dialog to resize itself for display, always showing the desired number of rows in the table, regardless of the font size used.
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Tim, Scott
Thank you. I will try it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Height of JTable