File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable - How to Delete Data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable - How to Delete Data" Watch "JTable - How to Delete Data" New topic
Author

JTable - How to Delete Data

Ajit Kumar
Ranch Hand

Joined: Jan 27, 2001
Posts: 81
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

Joined: Feb 24, 2001
Posts: 96
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

Joined: Feb 23, 2001
Posts: 6
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);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable - How to Delete Data