-3

i've read the javadoc of removeAll() method, but i don't understand so well what will happen. I need to remove all data contained in myJtable, and adding new data. removeAll will remove only data inside table?

that's how i've declared my jtable:

JTable table = new JTable(new DefaultTableModel(info, myHeader)) {
        @Override
        public boolean isCellEditable(int row, int column) {
            //disable table editing
            return false;
        }
    };
    header = table.getTableHeader();
    header.setBackground(Color.GREEN);
    JScrollPane scroll_pane = new JScrollPane(table);
    //hide column
    table.removeColumn(table.getColumnModel().getColumn(1));
    //scrollbar insert
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    //disable dragging and resizing column
    table.getTableHeader().setReorderingAllowed(false);
    table.getTableHeader().setResizingAllowed(false);
    table.addMouseListener(new JTableRowListener(table));

where args of JTable constructor are String arrays

mKorbel
  • 109,525
  • 20
  • 134
  • 319
giozh
  • 9,868
  • 30
  • 102
  • 183

1 Answers1

3

You want to change your JTable's model, not call removeAll(). Note that if you go to the JTable API you'll see that the removeAll() method is not a method specifically of JTable but rather of Container, one of JTable's parents. The method does not do "JTable-specific" actions, but rather removes all components held by a container, such as components held in a JPanel for an example.

To empty out a JTable you will want to do one of the following:

  • Remove all of the data held by the JTable's current model
  • or set the current model's row count to 0
  • or change the JTable's model to a new (empty) model via its setModel(...) method

The specifics of your solution will depend on the specifics of your current code, how you've set up your JTable and it's model, etc. For more information, you will want to show us your pertinent code (preferably an SSCCE) and give us more information about your program and its data, as well as details about current problems you may be having with your code.

For more, check out the JTable tutorial.


Edit

You've already been given the same link to the JTable tutorial in camickr's accepted answer to your previous JTable question. Perhaps it's time to consider studying the tutorial.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • i've edited my question. That's not the same question of that link, it's quite different – giozh Aug 11 '13 at 17:36
  • 1
    @giozh: Please re-read my answer. I never said that your previous question was the same as this one. Instead I said that the answer to your previous question also gave you the link to the JTable tutorial. Again, you should consider ***reading the tutorial***. All of this information is contained in the tutorial. – Hovercraft Full Of Eels Aug 11 '13 at 17:44
  • @giozh agree with HFOE, just once time to read DefaultTableModel API can you .... – mKorbel Aug 11 '13 at 18:19