gurbi
gurbi

Reputation: 163

JTable - how to get deleted rows

I'm using JTable with DefaultTableModel as model and DefaultListModel as underlying data within the model itself - so JTable is displaying the data, which is stored in this List.

In JTable I'm getting notification when elements are added/removed from the model (via fireTableRowsInserted and fireTableRowsDeleted fired by DefaultTableModel).

Is there some possibility to get to the content of the elements, which were deleted?

I do need to perform some cleanups when specific elements were deleted, but it seems, that fireTableRowsDeleted notification comes only after the deletion, which is too late.

---- EDIT: To get better picture, lets presume SSCCE looks like this:

TableDemo.java

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class TableDemo extends JPanel {

public TableDemo() {
    super(new GridLayout(1,0));

    JTable table = new JTable(new MyTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    JScrollPane scrollPane = new JScrollPane(table);

    add(scrollPane);
}

class MyTableModel extends AbstractTableModel {
    private String[] columnNames = {"Object Name"};

    private TableData data = new TableData();

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.getSize();
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data.elementAt(row);
    } 

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    } 

    public boolean isCellEditable(int row, int col) {         
        if (col < 2) {
            return false;
        } else {
            return true;
        }
    } 

    public void setValueAt(Object value, int row) {
        data.setElementAt(value, row);
        fireTableCellUpdated(row, 0);
    }
} 

private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TableDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    TableDemo newContentPane = new TableDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

TableData.java

import javax.swing.DefaultListModel;


public class TableData extends DefaultListModel {
public TableData() {
    for(int i = 0; i < 10; i++)
    super.addElement("Object " + i);        
}   
}

TableDemo is part of the UI level, TableData class is part of my Model level and is updated on the back-end. This data is then displayed on the front-end, but there is no interaction/edit functionality there. However, at some point application needs to react on some changes - like deletion of some elements, etc.

Is there some way, how to get notified about changes in the TableData before/when they happen? Or is my way of displaying the data not correct and I should use some other mechanism? (which one?)

One more piece of info not drawn here: I'm using also implementing ListDataListener in MyTableModel, so I'm listening to the changes in TableData, however, this is not sufficient.

Upvotes: 0

Views: 424

Answers (2)

D-Klotz
D-Klotz

Reputation: 2073

The way that I handle this, is that I force the user to either mark a check box that is within the row (the column is labeled "Delete") and I add a button or right click menu option that does the deletion event. The point being, that the action performed event is fired and within that call, I figure out what row is selected, retrieve the data, and go forward with the last step being the removal of the row.

----- EDIT -------

You also need to implement your own Abstract table model implementation so that you can implement the above. DefaultTableModel isn't really something I use for any serious coding, mainly because a lot of the control is removed.

Upvotes: 0

camickr
camickr

Reputation: 324118

There is no way to get the data once it is removed from the model unless you manage that functionality yourself.

You are probably using the removeRow(...) method from the DefaultTableModel.

If you want to track the rows before the are deleted, then you will need to override the removeRow(...) method and track the data before it is deleted.

Or if you are using some other custom TableModel you would need to override the method that deletes the row.

Upvotes: 1

Related Questions