JTable: Detect cell data change?

But it doesn't yet enable me to detect the old and the new value of this cell. What else do I have to do? It is easier to use a TableModelListener to listen for changes but it still has the problem of not being able to access the old value Check out the Table Cell Listener for a solution that gives you access to the "old value" as well as the "new value.

But it doesn't yet enable me to detect the old and the new value of this cell. What else do I have to do? It is easier to use a TableModelListener to listen for changes but it still has the problem of not being able to access the old value.

Check out the Table Cell Listener for a solution that gives you access to the "old value" as well as the "new value".

Thanks, it works fine! But I only want changes by the user. But with a TableModelListener I also get the changes by the computer during initialization.So a CellEditorListener would be better, right?

– Marco W. Jul 31 at 14:44 Did you read the link? I gave you a solution for acessing the "old value".

I was suggesting in general a TableModelListener is easier to use (compared to a CellEditorListener) because it is one listener for all types of data (String, Date, Integer, etc. ). You would just add the listener AFTER the data has been added to the model. – camickr Jul 31 at 14:50.

MKorbel is on to something. What if you create your own cell editor that extends DefaultCellEditor: customerTable. SetDefaultEditor(String.

Class, new DefaultCellEditor(new JTextField()){ @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { // code on line below is redundant but would be needed if need to see // other property of the value object than toString() String valueStr = (value == null)? "null" : value.toString(); System.out. Printf("%d, %d: %s%n", row, column, valueStr); return super.

GetTableCellEditorComponent(table, value, isSelected, row, column); } @Override public Object getCellEditorValue() { System.out. Printf("cell editor value: %s%n", super. GetCellEditorValue()); return super.

GetCellEditorValue(); } }).

Or TableModelLister with ListSelectionListener, fist returns if TableCell changed, second from selected call, then just compare Row&Coulumn Index from TableModelLister with ListSelectionListener import java.awt. *; import java.util. Random; import java.util.

Vector; import javax.swing. *; import javax.swing.event. ListSelectionEvent; import javax.swing.event.

ListSelectionListener; import javax.swing.event. TableModelEvent; import javax.swing.event. TableModelListener; import javax.swing.table.

AbstractTableModel; import javax.swing.table. TableCellRenderer; public class Forum implements ListSelectionListener { private JFrame frame = new JFrame("Frame"); private JPanel fatherCenter = new JPanel(); private JScrollPane tableScroll = new JScrollPane(); private myTableModel tableModel; private JTable dialogTable; private ListSelectionModel lsDialog; private void addComponentsToPane(Container pane) { tableModel = new myTableModel(); dialogTable = new JTable(tableModel) { private static final long serialVersionUID = 1L; @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component comp = super. PrepareRenderer(renderer, row, column); JComponent jc = (JComponent) comp;//for Custom JComponent if (!isRowSelected(row)) { int modelRow = convertRowIndexToModel(row); boolean type = (Boolean) getModel().

GetValueAt(modelRow, 2); boolean type1 = (Boolean) getModel(). GetValueAt(modelRow, 3); boolean type2 = (Boolean) getModel(). IsCellEditable(row, column); comp.

SetForeground(Color. Black); if ((type) && (!type1)) { comp. SetBackground(Color.

Yellow); } else if ((!type) && (type1)) { comp. SetBackground(Color. Orange); } else if ((!type) || (!type1)) { comp.

SetBackground(Color. Red); //} else if ((!type2)) { //comp. SetForeground(Color.

Red); //comp. SetBackground(Color. Magenta); } else { comp.

SetBackground(row % 2 == 0? GetBackground() : getBackground().darker()); } dialogTable. ConvertRowIndexToView(0); } else { comp.

SetForeground(Color. Blue); comp. SetBackground(Color.

LightGray); } if (!isCellEditable(row, column)) { comp. SetForeground(Color. Red); comp.

SetBackground(Color. Magenta); } return comp; } }; tableScroll = new JScrollPane(dialogTable, ScrollPaneConstants. VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.

HORIZONTAL_SCROLLBAR_AS_NEEDED); tableScroll. SetBorder(null); dialogTable.getTableHeader(). SetReorderingAllowed(false); dialogTable.

SetSelectionMode(ListSelectionModel. SINGLE_SELECTION); lsDialog = dialogTable. GetSelectionModel(); dialogTable.

PutClientProperty("terminateEditOnFocusLost", Boolean. TRUE); dialogTable. SetRowHeight(20); dialogTable.

SetRowMargin(2); ListSelectionModel rowSelMod = dialogTable. GetSelectionModel(); //ListSelectionModel colSelMod = dialogTable.getColumnModel(). GetSelectionModel(); rowSelMod.

AddListSelectionListener(this); //colSelMod. AddListSelectionListener(this); fatherCenter = new JPanel(); fatherCenter. SetLayout(new BorderLayout(10, 10)); fatherCenter.

Add(tableScroll, BorderLayout. CENTER); pane. Add(fatherCenter); } private void addData() { Runnable doRun1 = new Runnable() { @Override public void run() { tableModel.resetTable(); Vector tbl = new Vector(); Vector tbl1 = new Vector(); Random rnd = new Random(); tbl.

Add("Integer"); tbl. Add("Double"); tbl. Add("Boolean"); tbl.

Add("Boolean"); tbl. Add("String"); tableModel. SetColumnNames(tbl); for (int row = 0; row data; private Vector colNames; private boolean _columnsVisible = {true, true, true, true, true}; myTableModel() { this.

ColNames = new Vector(); this. Data = new Vector(); } myTableModel(Vector colnames) { this. ColNames = colnames; this.

Data = new Vector(); } public void resetTable() { this.colNames. RemoveAllElements(); this.data. RemoveAllElements(); } public void setColumnNames(Vector colNames) { this.

ColNames = colNames; this. FireTableStructureChanged(); } public void addRow(Vector data) { this.data. Add(data); this.

FireTableDataChanged(); this. FireTableStructureChanged(); } public void removeRowAt(int row) { this.data. RemoveElementAt(row); this.

FireTableDataChanged(); } @Override public int getColumnCount() { return this.colNames.size(); } @Override public Class getColumnClass(int colNum) { switch (colNum) { case 0: return Integer. Class; case 1: return Double. Class; case 2: return Boolean.

Class; case 3: return Boolean. Class; default: return String. Class; } } @Override public boolean isCellEditable(int row, int colNum) { switch (colNum) { case 2: return false; default: return true; } } @Override public String getColumnName(int colNum) { return this.colNames.

Get(colNum); } @Override public int getRowCount() { return this.data.size(); } @Override public Object getValueAt(int row, int col) { Vector value = this.data. Get(row); return value. Get(col); } @Override public void setValueAt(Object newVal, int row, int col) { Vector aRow = data.

ElementAt(row); aRow. Remove(col); aRow. InsertElementAt(newVal, col); fireTableCellUpdated(row, col); } public void setColumnVisible(int index, boolean visible) { this.

_columnsVisibleindex = visible; this. FireTableStructureChanged(); } } }.

Tme.getColumn() + " changed. Int col = dialogTable. For (int I = 0; I < rows.

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