I am trying to create a simple input verifier for JTable. I ended up redefining the method: editingStopped (). The problem is that the event does not contain information about the updated cell.
This is my "pseudo code":
If (user finished editing a cell) { Check if cell`s value is "1" or "0" or "-" (Karnaugh-Veitch) If (check = false) setValue (cell, ""); }
The first thing I tried was here:
table.getModel().addTableModelListener(new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { inputVerify (e.getColumn(), e.getFirstRow()); } }); public void inputVerify (int column, int row) { boolean verified = true; String field = table.getValueAt(row, column).toString(); if (field != null && field.length() == 1) { if ( !(field.charAt(0) == '0' || field.charAt(0) == '1' || field.charAt(0) == '-' )) verified = false; } else { verified = false; } if (!verified) { table.getModel().setValueAt("", row, column); java.awt.Toolkit.getDefaultToolkit().beep(); } System.out.println ("Column = " + column + " Row = " + row + " Value = " + table.getValueAt(row, column) +" Verified = "+verified); }
But it ends: StackOverflow Exception. I think the problem is that: setValueAt (..) fires another tableChanged () event and an infinite loop is generated.
Now I tried this here:
table.getDefaultEditor(Object.class).addCellEditorListener(new CellEditorListener() { // called when editing stops public void editingStopped(ChangeEvent e) { // print out the value in the TableCellEditor System.out.println(((CellEditor) e.getSource()).getCellEditorValue().toString()); } public void editingCanceled(ChangeEvent e) { // whatever } });
But, as you can see, I can just get the new cell value, not the "coordinate". I need to call the setValueAt (..) method, but I do not know how to get the coordinates of the cell.
Or is there an easier way to create an input verifier?
Regards Ioannis K.
source share