• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable - How to Delete Data

 
Ajit Kumar
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I would appreciate any information on how to delete data rows from a JTable.
Also, how do i add new data to a JTable.
(OR)
Pls. direct me to a tutorial or an article on it.
Thanks in advance
 
Brett Spell
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ajit,
First, let me clarify that you can't add or remove rows from a JTable, but rather, you add or remove them from the TableModel that supplies the JTable with the data it displays.
Having said that, the way that you add and remove rows depends upon the TableModel implementation that you're using. DefaultTableModel does include addRow(), insertRow(), and removeRow() methods, but for reasons that I won't go into here, you should avoid using DefaultTableModel.
My guess is that you've created your own model class that extends AbstractTableModel, and if that is the case, your code is already responsible for maintaining the data that is displayed in the JTable. I'm going to further assume that you've already added code that allows you to add or remove one or more rows from the data that your class maintains, and that you're just asking how to make those changes appear in the JTable.
Once you have code that allows you to insert and delete rows from your TableModel, you need to use the fireTableRowsDeleted() and fireTableRowsInserted() methods when you delete and insert rows, respectively. Those methods will cause an event notification to be sent to the TableModel's registered listeners (the JTable itself is probably the only listener) notifying them that the table data has changed.
Here's an example of how you might provide the ability to add rows in a subclass of AbstractTableModel:

------------------
Brett Spell
Author, Professional Java Programming
[This message has been edited by Brett Spell (edited February 26, 2001).]
[This message has been edited by Brett Spell (edited February 26, 2001).]
 
sandhya raviprasad
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ajith ..
This was the code I tried to insert a new row and add in data statically into it. Hope this helps. Regarding deleting a row, I tried the following code, but it only eliminates the data, not the row itself.You could search for JTable in the sun's web site I guess you can find something.
// deleting a row ...
dataEntrydel = new Object[dataEntries.length][3];
int sizedel = dataEntrydel.length;

for(int i = 0; i != dataEntries.length; i++)
for(int j = 0; j != 3; j++)
{
dataEntrydel[i][j] = dataEntries[i][j];
if(JTable1.isRowSelected(i))
{
dataEntrydel[i][j] = "";
sizedel = sizedel - 1;

}

}
dataEntries = dataEntrydel;
JTable1.setModel(new EditableTableModel(columnTitles,dataEntries));
JTable1.createDefaultColumnsFromModel();
JPanel1.add(JTable1);
//===============================================================
// Inserting a row
dataEntries & dataEntrynew are Object arrays.
dataEntrynew = new Object[dataEntries.length+1][3];
int k;
for(int i = 0; i != dataEntries.length; i++)
for(int j = 0; j != 3; j++)
{
dataEntrynew[i][j] = dataEntries[i][j];
//System.out.println("dataEntrynew[" +i + "][" + j +"] -> " + dataEntrynew[i][j]);

}
try
{
for( k = 0; k != 3; k++)
{
String str1 = "Indiana";
String str2 = "144.50.50.1";
dataEntrynew[dataEntries.length][0] = str1;
dataEntrynew[dataEntries.length][1] = str2;
dataEntrynew[dataEntries.length][2] = str2;

}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Error");
}
// Assign the new array to the old array
dataEntries = dataEntrynew;
JTable1.setModel(new EditableTableModel(columnTitles,dataEntries));
JTable1.createDefaultColumnsFromModel();
JPanel1.add(JTable1);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic