Remove rows from JTable?

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.

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

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.. – somya agrawal Jul 13 '09 at 8:50 1 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.

– camickr Jul 13 '09 at 15:51 Thank you for including working code! – macias Oct 29 '11 at 20:58.

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.

Thanx for the help, but I want to know how to remove rows from the table model , I m not getting any method . – somya agrawal Jul 13 '09 at 6:18 1 Have a read through things like: java.sun.com/docs/books/tutorial/uiswing... – dave Jul 13 '09 at 6:26 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. – dave Jul 13 '09 at 6:29.

Look at the DefaultTableModel for a simple model that you can use: java.sun.com/javase/6/docs/api/javax/swi... 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.

java.sun.com/javase/6/docs/api/javax/swi... Look at the basic Sun tutorial for more information on using the JTable with the table model: java.sun.com/docs/books/tutorial/uiswing....

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.

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 columnNames = new Vector(); // Each value in the vector is a row; String - row data; private Vector data = new Vector(); ... 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 arr = new ArrayList(); public void removeRow(int row) { data.

Remove(row); } HashMap: //Integer - row number; String - row data; HashMap data = new HashMap(); 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.

Mmm is very simple guys for( int I = model.getRowCount() - 1; I >= 0; i-- ) { model. RemoveRow(i); }.

1 Nothing like million notifications at the morning ;-) – macias Oct 29 '11 at 20:56.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions