| View previous topic :: View next topic |
| Author |
Message |
thealy Posted via mailing list.
|
Posted: Tue Feb 24, 2009 8:00 pm Post subject: Add row to JTable |
|
|
I'm using NB 6.5 to create a small project that contains a JTable. I
need to add (append) a series of new rows at the bottom of the JTable
based on user input in a TextArea, as triggered by a button push.
The TableModel is created by NB. If I configure the table with, say, 4
rows I am able to change the data, but not add new rows. I've looked
through the tutorials but can't find a solution which fits.
Any suggestions? |
|
| Back to top |
|
 |
Bayless Kirtley Posted via mailing list.
|
Posted: Tue Feb 24, 2009 10:40 pm Post subject: Add row to JTable |
|
|
You'll have to create your own table model. Probably can just use the
DefaultTableModel which, I believe does have a addRow or nsertRow
method. It's really pretty easy. Refer to the Javadac for specifics.
Bayless
----- Original Message -----
From: "thealy" <address-removed>
To: <address-removed>
Sent: Tuesday, February 24, 2009 2:00 PM
Subject: [nbusers] Add row to JTable
| Quote: | I'm using NB 6.5 to create a small project that contains a JTable. I
need to add (append) a series of new rows at the bottom of the JTable
based on user input in a TextArea, as triggered by a button push.
The TableModel is created by NB. If I configure the table with, say, 4
rows I am able to change the data, but not add new rows. I've looked
through the tutorials but can't find a solution which fits.
Any suggestions? |
|
|
| Back to top |
|
 |
pointer2null
Joined: 11 Feb 2009 Posts: 13
|
Posted: Wed Feb 25, 2009 8:36 pm Post subject: |
|
|
You will need to make your own table model as the one used by default in NB doesn't have the addRow method.
A table model is simple to do:
| Code: |
// define new table model class
class SearchResultsModel extends DefaultTableModel {
private String[] colNames;
private Object[][] rowData;
SearchResultsModel(Object[][] rowData, String[] colNames){
super(rowData, colNames);
this.colNames = colNames;
this.rowData = rowData;
}
@Override
public Class getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
@Override
public String getColumnName(int col) {
return colNames[col];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false; // note my table does not allow editing
// if yours does you need to change this.
}
}
|
Then when you need to add a line:
| Code: |
Object[] newRowData = {new Integer(0), "", new Double(0), new Double(0), new Double(0)};
myNewTableModelInstance.addRow(newRowData);
|
Obviously your newRowData will contain the right object types for your table. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|
|
|