-
November 15th, 1999, 07:09 AM
#1
how to clear all row in a jtable ?
how do i clear all the rows in a jtable so that i can put in new rows as a result of clicking on a button ?
http://members.xoom.com/lookads
-
November 15th, 1999, 07:16 AM
#2
Re: how to clear all row in a jtable ?
U will have a ur own TableModel for keeping all the data. U can have a method for that Model which removes all the items in all cells of JTable. And on action event of desired button u can call this method of ur TableModel.
U can't say 'jtable.clear()' or something as it is rather tightly bound with the TableModel u have for the table.
- UnicMan
http://members.tripod.com/unicman
-
November 15th, 1999, 08:34 PM
#3
Re: how to clear all row in a jtable ?
i have no idea how to start doing that.
can you give me some code sampels ?
thanks a lot.
http://members.xoom.com/lookads
-
November 17th, 1999, 12:47 AM
#4
Re: how to clear all row in a jtable ?
Well.. I can't just create the source from scratch. I will give u an idea instead.
For every JTable there is its datamodel (i.e. TableModel). If u want to show something in JTable, u should have created a sub-class of 'AbstractTableModel'. And attached the model to the JTable.
Now say the sub-class is 'MyTableModel'.
Now this class will look similar to...
class MyTableModel extends AbstractTableModel
{
public int getColumnCount(...) ...
public int getValueAt(...) ...
....
// Here u add ur method to remove all the elements from ur internal storage
public void clear()
{
// all the code for removing all elements from this data model is here.
}
}
Now when u want to clear the JTable u know that u have attached 'MyTableModel' to JTable. So u can do following ....
MyTableModel mtm = (MyTableModel)jtable.getModel(); // Type cast it to ur class
mtm.clear(); // This will remove all data from the model
jtable.repaint(); // This will reflect the changes (empty table) on the screen
Hope u got the concept.
- UnicMan
http://members.tripod.com/unicman
-
November 17th, 1999, 01:06 AM
#5
Re: how to clear all row in a jtable ?
> how do i clear all the rows in a jtable so that i can put in new rows as a result of clicking on
> a button ?
Your question is not clear newkid .. Do you like to "CLEAR" the rows ? or do you like to
"REMOVE" all rows ???
If you like to remove all rows in your JTable.. try this..
table.setNumRows(0); // this will erase all the contents in your data vector
// and set data vector's size to 0
or if you want to clear the rows ...
for( int nRow = 0 ; nRow < table.getRowCount() ; nRow++ ){
for( int nColumn = 0 ; nColumn < table.getColumnCount(); nColumn++ ){
table.setValueAt("" , nRow , nColumn );
}
}
-
November 17th, 1999, 01:26 AM
#6
Re: how to clear all row in a jtable ?
> table.setNumRows(0);
I am sorry ... setNumRows() is member function of DefaultTableModel
((DefaultTableModel)table.getModel()).setNumRows(0);
-
November 17th, 1999, 06:12 AM
#7
how to do calculations with calender ?
tfStartDate.setText("30/05/1998");
tfEndDate.setText("30/03/1999");
String stryear1 = tfStartDate.getText().substring(6,10) ;
String strmonth1 = tfStartDate.getText().substring(3, 5) ;
String stryear2 = tfEndDate.getText().substring(6,10) ;
String strmonth2 = tfEndDate.getText().substring(3, 5) ;
int year1 = java.lang.Integer.parseInt(stryear1);
int year2 = java.lang.Integer.parseInt(stryear2);
int month1 = java.lang.Integer.parseInt(strmonth1);
int month2 = java.lang.Integer.parseInt(strmonth2);
int nomonth = (year2-year1)*12 + (month2-month1) + 1;
// int nomonth = month2 - month1 + 1; // no of rows to display
String colyearmonth[] = new String[nomonth];
int ia=0;
for (ia=0;ia<=nomonth-1;ia++) {
/* how to store the string date here ?
ie
1998-05
1998-06
1998-07
..
1999-03
*/
}
http://members.xoom.com/lookads
-
August 7th, 2010, 03:58 PM
#8
Re: how to clear all row in a jtable ?
public void deleteAllRows( DefaultTableModel model )
{
for( int i = model.getRowCount() - 1; i >= 0; i-- )
{
model.removeRow(i);
}
}
-
November 23rd, 2012, 06:30 PM
#9
Re: how to clear all row in a jtable ?
The simplest way to remove all rows from JTable, just use this method instead...
tablemodel.getDataVector().removeAllElements();
tablemodel.fireTableDataChanged();
tablemodel is the model which you created for your table to add new rows. This is the shortest and fastest way of deleting all rows because what if you got thousands of rows? looping????
-
November 25th, 2012, 07:30 AM
#10
Re: how to clear all row in a jtable ?
This will only work if your model is DefaultTableModel or a sub class of it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|