0

I'm trying to set a key listener to a specific column in a table shown as : |Quantity|Price| i'm using this code bellow:

private void tableKeyPressed(java.awt.event.KeyEvent evt) {                                 
    int keycode = evt.getKeyCode();
    if (keycode == KeyEvent.VK_ENTER) {
        pricetext.setText(Integer.toString(calculatesum()));
    }         
} 

the problem is when i modify the quantity i have to press "Enter" twice so the function inside the key listener method. (i assume the 1st enter pressed is meant for the column quantity and the 2nd is meant for the table).

thanks for help

Fennec
  • 13
  • 4
  • 1
    if your case is to watch value changes on table cells then you should have a look at [here](https://stackoverflow.com/questions/12823475/what-is-the-best-way-to-listen-for-changes-in-jtable-cell-values-and-update-data) and [here](https://tips4java.wordpress.com/2009/06/07/table-cell-listener/) – guleryuz May 18 '19 at 16:07
  • *(i assume the 1st enter pressed is meant for the column quantity and the 2nd is meant for the table* - the first key press is handled by the TableCellEditor, which currently has focus and will cause the value in the editor to be saved to the model and the editor removed from the table. Now that the editor has been removed from the table, the second enter will be handled by the table. – camickr May 18 '19 at 17:22
  • You have two options: 1) add a TableModelListener to the TableModel. It will generate an event whenever the model is changed. See: https://stackoverflow.com/questions/3540661/tablemodellistener-and-multiple-column-validation/3541876#3541876 for a working example. 2) override the setValueAt(...) method of your TableModel to do the calculation when the model in updated. – camickr May 18 '19 at 17:22
  • @guleryuz thans for the tip – Fennec May 19 '19 at 12:40
  • @camickr thanks i added a TableModelListener as shown in example and it solved the problem – Fennec May 19 '19 at 12:40

0 Answers0