How to get value of a JTable cell during edit
10,607
Solution 1
Use a DocumentListener
, illustrated here, or a DocumentFilter
, seen here, in your TableCellEditor
, shown here.
Solution 2
HINT :
if (jTable1.getCellEditor() == null) {
System.out.println("Not Edited");
} else {
System.out.println(jTable1.getValueAt(jTable1.getSelectedRow(),jTable1.getSelectedColumn()));
}
where jTable1 is your JTable Name
Related videos on Youtube
Author by
Mark09
Updated on September 27, 2022Comments
-
Mark09 over 1 year
The solution I have seen so far is listen cell change i.e.
TableModelListener tableModelListener = new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { if (e.getType() == TableModelEvent.UPDATE) { row = e.getFirstRow(); col = e.getColumn(); // do something } } };
But I need to get the cell value, selectedRow & selectedColumn when typing, and before hitting enter. How to do it?
-
kleopatra over 11 yearsI need to get the cell value, selectedRow & selectedColumn when typing, and before hitting enter - sounds weird: it doesn't change while editing, so you can grab it before starting the edit ...
-
-
Mark09 over 11 yearsMy needs is exactly same as Mubashir: This requires pressing enter key, what i require is that when the value is changed the event automatically fires. I am checking the DocumentEvent now.
-
Mark09 over 11 yearsMubashir is the thread replyier in your TableCellListener link who replied on March 19, 2012
-
Mark09 over 11 yearsThank you for your information but I am still new to Java and need more time to understand these.
-
trashgod over 11 yearsYou definitely need a
CellEditor
, which will tell yourow
&column
; don't hesitate to update your question going forward. -
Neigyl R. Noval almost 10 years@trashgod, please provide example on where to place the
DocumentListener
on the extendedDefaultCellEditor
based on the link you provided. What method do I need to override? I have the same problem. Thanks. -
trashgod almost 10 yearsAdd the listener to the text component used by the
CellEditor
, which isJTextField
in the example cited. Ping me if you pose a new question. -
Kyle over 8 yearsThere is nothing wrong with this answer, not exactly suited for your particular needs (as they are hard to achieve anyway) but it helped me
-
m4heshd almost 5 yearsThis is exactly what OP doesn't want. This only fires the event when the editing has stopped.