How to remove a row from JTable? [closed]
Asked Answered
O

6

18

I want to remove some rows from a JTable. How can I do it?

Oatis answered 13/7, 2009 at 6:2 Comment(1)
Tip: if you explain what you have already tried, and what on-line tutorials you've read, people will be more inclined to help you. Or give some sample code and tell us which bit doesn't work. Read this if you haven't already: java.sun.com/docs/books/tutorial/uiswing/components/table.htmlKrusche
S
39

In order to remove a row from a JTable, you need to remove the target row from the underlying TableModel. If, for instance, your TableModel is an instance of DefaultTableModel, you can remove a row by doing the following:

((DefaultTableModel)myJTable.getModel()).removeRow(rowToRemove);
Stirling answered 13/7, 2009 at 6:29 Comment(2)
thanx for the help,but i have used this method and it is not working. actually my JTable is using LocationTable model(own created) extendind abstract table model so when i am using removeRow(i) method it is giving error as "cant resolve removeRow()" ..what alternative shall i use..Oatis
Well if you created your own custom TableModel then you are responsible for creating your own "removeRow" method. Look at the source code of the DefaultTableModel to see how this is done. The easy solution is to use the DefaultTableModel. Why do you think you need to create a custom TableModel? Until you understand the basics of using models use the defaults provided.Scalariform
G
9

If you need a simple working solution, try using DefaultTableModel.

If you have created your own table model, that extends AbstractTableModel, then you should also implement removeRow() method. The exact implementation depends on the underlying structure, that you have used to store data.

For example, if you have used Vector, then it may be something like this:

public class SimpleTableModel extends AbstractTableModel {
    private Vector<String> columnNames = new Vector<String>();
    // Each value in the vector is a row; String[] - row data;
    private Vector<String[]> data = new Vector<String[]>();

    ...

    public String getValueAt(int row, int col) {
        return data.get(row)[col];
    }

    ...

    public void removeRow(int row) {
        data.removeElementAt(row);
    }
}

If you have used List, then it would be very much alike:

// Each item in the list is a row; String[] - row data;
List<String[]> arr = new ArrayList<String[]>();

public void removeRow(int row) {
    data.remove(row);
}

HashMap:

//Integer - row number; String[] - row data;
HashMap<Integer, String[]> data = new HashMap<Integer, String[]>();

public void removeRow(Integer row) {
    data.remove(row);
}

And if you are using arrays like this one

String[][] data = { { "a", "b" }, { "c", "d" } };

then you're out of luck, because there is no way to dynamically remove elements from arrays. You may try to use arrays by storing separately some flags notifying which rows are deleted and which are not, or by some other devious way, but I would advise against it... That would introduce unnecessary complexity, and would in fact just be solving a problem by creating another. That's a sure-fire way to end up here. Try one of the above ways to store your table data instead.

For better understanding of how this works, and what to do to make your own model work properly, I strongly advise you to refer to Java Tutorial, DefaultTableModel API and it's source code.

Gavrah answered 14/7, 2009 at 7:16 Comment(0)
F
5

mmm is very simple guys

for( int i = model.getRowCount() - 1; i >= 0; i-- )
{
    model.removeRow(i);
}
Fleischman answered 27/12, 2010 at 0:36 Comment(1)
Nothing like million notifications at the morning ;-)Krypton
A
4

The correct way to apply a filter to a JTable is through the RowFilter interface added to a TableRowSorter. Using this interface, the view of a model can be changed without changing the underlying model. This strategy preserves the Model-View-Controller paradigm, whereas removing the rows you wish hidden from the model itself breaks the paradigm by confusing your separation of concerns.

Alcoran answered 13/7, 2009 at 14:53 Comment(0)
A
3

A JTable normally forms the View part of an MVC implementation. You'll want to remove rows from your model. The JTable, which should be listening for these changes, will update to reflect this removal. Hence you won't find removeRow() or similar as a method on JTable.

Aureliaaurelian answered 13/7, 2009 at 6:10 Comment(3)
thanx for the help, but i want to know how to remove rows from the table model , i m not getting any method .Oatis
Have a read through things like: java.sun.com/docs/books/tutorial/uiswing/components/table.htmlAureliaaurelian
In brief, you create a custom TableModel. JTable doesn't store the rows, it delegates that to a TableModel. You can subclass from AbstractTableModel if you wish. Call fireTableRowsDeleted() every time you delete rows.Aureliaaurelian
C
3

Look at the DefaultTableModel for a simple model that you can use:

http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html

This extends the AbstractTableModel, but should be sufficient for basic purposes. You can always extend AbstractTableModel and create your own. Make sure you set it on the JTable as well.

http://java.sun.com/javase/6/docs/api/javax/swing/table/AbstractTableModel.html

Look at the basic Sun tutorial for more information on using the JTable with the table model:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#data

Conventioneer answered 13/7, 2009 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.