I am working on a GUI application in Java for a client. One of the parts of that GUI needs to upload about a 1000 records each having 17 attributes simultaneously(i.e. it needs a 1000 X 17 table). Now Netbeans IDE 7.2.1 allows at most 100 rows at a time in a Jtable. Any suggestions how can i make one for displaying 1000 entries at a time. I considered having 10 tables one after other but that will leave a very messy coding to be done later at the back end!
-
5Patient: "Doctor, Netbeans won't let me add more than 100 rows to a JTable." Doctor: "Then code your JTable directly, instead of using the IDE!". – FoggyDay Jun 16 '14 at 17:20
2 Answers
Don't use an IDE to create your GUI. The IDE generates terrible code that creates a table with the null values for the number of rows that you want to create.
There is no restriction on the number of rows a table can hold. If you create the code manually you can do something simple like:
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
JTable table = new JTable(model);
which will create an empty table with the column names that you specify
Then you can add rows individually using the DefaultTableModel.addRow(...)
method.
Or you can add all the rows at one time by using the DefaultTableModel.setDataVector(...) method.

- 321,443
- 19
- 166
- 288
-
1"Don't use an IDE to create your GUI. The IDE generates terrible code .. " That is wrong! – isaias-b Jun 16 '14 at 18:17
-
For some IDEs the code which is generated is really ugly but for the better ones it is more or less the same as handwritten. Take a look at the [google window builder eclipse plugin](http://stackoverflow.com/a/13671987/3165552). IMO it produces beautiful code and allows round trip code editing. But for tables i wouldn't use the gui guilder as well, but for wiring tables into controls. – isaias-b Jun 16 '14 at 18:24
You can create your GUI with any IDE you like. There is no problem with Netbeans in this area. Netbeans, like any other editing tool, allows you perfectly well to create some class like i.e. MyModel that extends an AbstractTableModel, which has no GUI and you should use in order to separate your Model from the View and Controller that have a GUI.
Your JTable will then automatically call getValueAt(row, col) and getRowCount() in order to show the tiny subset of your 1.000 or maybe 1.000.000 lines that need to be displayed.
You must not necessarily load all 1000 records to any Vector or ArrayList. Just make getValueAt(row, col) read each row and return every column.
There is a high probability that your user will not scroll down every time and will not ask the TableModel to provide anything more than the 40-60 lines that should be visible after the first rendering.
This example shows the getValueAt, used on a scrollable ResultSet sr from a database:
@Override
public Object getValueAt(int row, int column) {
try {
sr.absolute(row + 1); // position your ResultSet.
switch (column) {
case 0:
return sr.getInt("...");
case 1:
return sr.getString("...");
case 2:
return sr.getString("...");
case 3:
return ......
default:
return ("-");
}
} catch (SQLException sex) {
........
}
}
This is what happens at the GUI side:
- Is the JTable. Since you use a TableModel it will show you the JTable only at runtime.
- Here is the place that allows you to bind your model to the auto-generated code of NetBeans.
- This is the instance of your model. You could also write something like "new MyModel()" here.
The generated code is no monster either:
...
jTable1.setModel(etb);
jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
myMouseClicked(evt);
}
});
jScrollPane1.setViewportView(jTable1);
...

- 13,680
- 3
- 46
- 47