clear rows of JTable

Discussion in 'Java' started by pat270881, Apr 5, 2005.

  1. pat270881

    pat270881 Guest

    Hello,

    I have a JFrame with a JTable where some lines with two columsn are
    displayed. I want to write a function where all rows of the table are
    cleared. My JTable has a DataTableModel model. So I thought I have to
    remove the rows of the model to delete the rows from the JTable with
    the following code:

    for (int i = 0; i < model.getRowCount(), i++)
    {
    model.remove(i);
    }

    But when I use this code in that way, two rows of the JTable are not
    deleted.

    Can anybody help me with this problem?

    thx

    pat
    pat270881, Apr 5, 2005
    #1
    1. Advertising

  2. Hi,

    Indeed, imagine that your model contains (in this order) : A, B and C
    Your getRowCount equals 3

    loop 1 : remove index 0 , A (B comes to index 0 , C comes to index 1)
    loop 2 : remove index 1 , C
    loop 3 : remove index 2 , but there is nothing at index 2 anymore !

    Here you can see that B has never been removed.
    The above demonstration is true , for instance, with a Vector based model.

    If this is your problem, please consider using :

    while (model.getRowCount()>0){
    model.remove(0);
    }

    Regards,

    Arnaud




    "pat270881" <> a écrit dans le message news:
    ...
    > Hello,
    >
    > I have a JFrame with a JTable where some lines with two columsn are
    > displayed. I want to write a function where all rows of the table are
    > cleared. My JTable has a DataTableModel model. So I thought I have to
    > remove the rows of the model to delete the rows from the JTable with
    > the following code:
    >
    > for (int i = 0; i < model.getRowCount(), i++)
    > {
    > model.remove(i);
    > }
    >
    > But when I use this code in that way, two rows of the JTable are not
    > deleted.
    >
    > Can anybody help me with this problem?
    >
    > thx
    >
    > pat
    Arnaud Berger, Apr 5, 2005
    #2
    1. Advertising

  3. pat270881

    Nigel Wade Guest

    pat270881 wrote:

    > Hello,
    >
    > I have a JFrame with a JTable where some lines with two columsn are
    > displayed. I want to write a function where all rows of the table are
    > cleared. My JTable has a DataTableModel model. So I thought I have to
    > remove the rows of the model to delete the rows from the JTable with
    > the following code:
    >
    > for (int i = 0; i < model.getRowCount(), i++)
    > {
    > model.remove(i);
    > }
    >
    > But when I use this code in that way, two rows of the JTable are not
    > deleted.
    >
    > Can anybody help me with this problem?
    >
    > thx
    >
    > pat


    What is DataTableModel?

    DefaultTableModel.setRowCount(0) would do what you want.

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
    Nigel Wade, Apr 5, 2005
    #3
  4. pat270881

    natihern

    Joined:
    May 12, 2006
    Messages:
    1
    Remove all the rows from a JTable

    I used this sentence:

    DefaultTableModel dm = (DefaultTableModel)table.getModel();
    dm.getDataVector().removeAllElements();

    and it worked.:-D


    natihern, May 12, 2006
    #4
  5. pat270881

    Abdul Jabbar WebBestow

    Joined:
    Nov 23, 2012
    Messages:
    1
    Simplest Way to Remove all Rows from 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????

    :congrats:
    Abdul Jabbar WebBestow, Nov 23, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tivo Escobar
    Replies:
    1
    Views:
    7,274
    manusa
    Apr 12, 2007
  2. Tintin92
    Replies:
    1
    Views:
    1,675
    Andrew Thompson
    Feb 14, 2007
  3. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    998
    Mark Fitzpatrick
    Jan 31, 2008
  4. InvalidLastName

    Unrecognized element 'add' after <clear></clear>

    InvalidLastName, Feb 26, 2007, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    926
    Steven Cheng[MSFT]
    Mar 6, 2007
  5. Haircuts Are Important
    Replies:
    3
    Views:
    300
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page