aspose file tools*
The moose likes Java in General and the fly likes JTable static size problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "JTable static size problems" Watch "JTable static size problems" New topic
Author

JTable static size problems

Jamie Wool
Ranch Hand

Joined: May 03, 2004
Posts: 49
Hi all,
I have a jTable in netbeans, basically i have used the model feature to define the column headers and i create a set number of rows, say for this example 50. The problem i'm having is that this is pretty static and if i end up with more than 50 records i get an exception error. I could simple extented the rows but it seems a poor fix.

Is there a way to let the size of the jTable be changable, please remeber i'm using netbeans, so it's not so easy to edit the data directly.

Cheers

James
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
This must be something specific to the tools you are using. I don't think it is possible to set a static number of rows in a JTable (without creating your own table model).

What is the exception you are getting?
Jamie Wool
Ranch Hand

Joined: May 03, 2004
Posts: 49
Getting an index out of bounds because the program is attempting to write to row 51 of a table with only 50 rows.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
You must be using some custom table model, the DefaultTableModel uses a vector to store data, so you wouldn't get an IndexOutOfBounds error.

Could you post the stack trace, or at least the first 10-15 lines of it if it's really long.
Jamie Wool
Ranch Hand

Joined: May 03, 2004
Posts: 49
Here's the code that netbeans generates

salesBrowseResultsTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{new Integer(12), "Rose", "el rosia", "Goodhew", new Integer(56), new Float(6.99), "spato", "red", "Summer", new Integer(56), new Integer(12)},
{new Integer(16), "Tree", "el tree", "oak", new Integer(1), new Float(100.0), "treeus", "green", "summer", new Integer(1000), null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
},
new String [] {
"Shrub Id", "Shrub Name", "Latin Name", "Variety", "In Stock", "Price", "Genus", "Colour", "Flowering", "Height", "Width"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Float.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
};
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
That looks ok, as far as generated code goes. My guess is that where you are trying to add data it's calling the method
DefaultTableModel.insertRow(int row, Object[] data);
Where it should be calling
DefaultTableModel.addRow(Object[] data);

If it was me I'd get away from the netbeans generated code and just hand write it.

P.S. when you post blocks of code like that you should sound it with code tags. Look for the code button under where you write your post.
[ May 02, 2005: Message edited by: Steven Bell ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable static size problems