how to clear all row in a jtable ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: how to clear all row in a jtable ?

  1. #1
    Join Date
    Oct 1999
    Location
    usa
    Posts
    33

    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

  2. #2
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    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

  3. #3
    Join Date
    Oct 1999
    Location
    usa
    Posts
    33

    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

  4. #4
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    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

  5. #5
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    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 );
    }
    }





  6. #6
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    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);


  7. #7
    Join Date
    Oct 1999
    Location
    usa
    Posts
    33

    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

  8. #8
    Join Date
    Aug 2010
    Posts
    1

    Red face 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);
    }
    }

  9. #9
    Join Date
    Nov 2012
    Posts
    1

    Thumbs up 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????


  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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 code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center