This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
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 Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Height of JTable" Watch "Height of JTable" New topic

Height of JTable

Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
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.
Scott Pedigo

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
"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

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.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.
jQuery in Action, 2nd edition
subject: Height of JTable