2

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?

Amarnath
  • 8,736
  • 10
  • 54
  • 81
Mark09
  • 263
  • 1
  • 4
  • 9
  • 2
    _I 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 ... – kleopatra Jan 27 '13 at 13:52

3 Answers3

2

Use a DocumentListener, illustrated here, or a DocumentFilter, seen here, in your TableCellEditor, shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you for your information but I am still new to Java and need more time to understand these. – Mark09 Jan 27 '13 at 14:53
  • You definitely need a `CellEditor`, which will tell you `row` & `column`; don't hesitate to update your question going forward. – trashgod Jan 27 '13 at 18:27
  • @trashgod, please provide example on where to place the `DocumentListener` on the extended `DefaultCellEditor` based on the link you provided. What method do I need to override? I have the same problem. Thanks. – Neigyl R. Noval May 21 '14 at 14:37
  • Add the listener to the text component used by the `CellEditor`, which is `JTextField` in the example cited. Ping me if you pose a new question. – trashgod May 21 '14 at 17:39
1

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

c.pramod
  • 606
  • 1
  • 8
  • 22
  • the question is a bit strange, but whatever it really wants - this isn't an anser ;-) – kleopatra Jan 27 '13 at 13:54
  • There is nothing wrong with this answer, not exactly suited for your particular needs (as they are hard to achieve anyway) but it helped me – Kyle Aug 18 '15 at 15:44
0

Use TableCellListener for listening to the changes in the TableModel. By using this you can get the row-index, column-index, old-value and new-value of the edited cell in the table.

Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • 1
    My 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 Jan 26 '13 at 19:20
  • Mubashir is the thread replyier in your TableCellListener link who replied on March 19, 2012 – Mark09 Jan 27 '13 at 14:26
  • This is exactly what OP doesn't want. This only fires the event when the editing has stopped. – m4heshd Jul 15 '19 at 22:25