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

Share:
10,607

Related videos on Youtube

Mark09
Author by

Mark09

Updated on September 27, 2022

Comments

  • Mark09
    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
      kleopatra over 11 years
      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 ...
  • Mark09
    Mark09 over 11 years
    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
    Mark09 over 11 years
    Mubashir is the thread replyier in your TableCellListener link who replied on March 19, 2012
  • Mark09
    Mark09 over 11 years
    Thank you for your information but I am still new to Java and need more time to understand these.
  • trashgod
    trashgod over 11 years
    You definitely need a CellEditor, which will tell you row & column; don't hesitate to update your question going forward.
  • Neigyl R. Noval
    Neigyl R. Noval almost 10 years
    @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.
  • trashgod
    trashgod almost 10 years
    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.
  • Kyle
    Kyle over 8 years
    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
  • m4heshd
    m4heshd almost 5 years
    This is exactly what OP doesn't want. This only fires the event when the editing has stopped.