This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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).]
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.