Results 1 to 6 of 6
  1. #1
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default How to clear data in Jtable in JScrollPane

    Hi All,

    I have a jTable in a JScrollPane.I want to clear all the data in Jtable if i click on a menu item.I tried clearing it by first fetching the data model and then removing all its elements.

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

    Sometimes data is removed but most of the times elements are removed very slowly.Also if a window is opened on table data taht area of jtable gets cleared which seems very wierd.I want them to be removed the quickly when I click on clear.

    Anyone know the solution for this

    Thanks in advance,
    simmi

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One possible solution: just create a new empty DefaultTableModel object and set your table's model to this.

    If this doesn't help, you may wish to show us compilable code. We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem, in other words, an SSCCE (Short, Self Contained, Correct (Compilable), Example). For more info on SSCCEs please look here:

    [http://homepage1.nifty.com/algafield/sscce.html

    Again, if the code is compilable and runnable more people will be able to help you.

    Much luck!

  3. #3
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Java Code:
    dm.setDataVector(null,colhead);

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default

    Hi All,

    Thanks for your suggstions.I am able to clear the data using following code.

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

    Simmi :)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    This should also do it, without the need to explicitly repaint the table (untested)
    Java Code:
    ((DefaultTableModel)table.getModel()).setRowCount(0);
    db

Similar Threads

  1. Replies: 3
    Last Post: 01-11-2009, 11:20 PM
  2. [SOLVED] Cant display data in jTable
    By tpyq in forum NetBeans
    Replies: 2
    Last Post: 12-09-2008, 02:42 PM
  3. how to refresh data of the JTable
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-17-2008, 12:01 PM
  4. How to change data in jtable?
    By chandu.v in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2008, 09:46 AM
  5. how to clear the data of an object
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:50 PM

Posting Permissions

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