Question

I have two JTables filled with data on a JPanel. An user is trying to delete a row from one of the tables by selecting the row and clicking on the "delete" button. The problem for me is how to figure out which one of the two JTables was focused the last ( user could click on row in table 1 then on row in table 2, then "delete" and I want to delete row from table 2 ). I would see it maybe this way:

tables have added ListSelectionListener that fires event valueChanged, when a row is selected, but now I have to somehow notify the component that contains those two tables which one was selected the last

Does anyone know how can I solve this? And please if u could deliver some example code it would be greatly appreciated.

Was it helpful?

Solution

You can use the 'getSource' method of the event you're receiving to work out which table generated the event. For example,

public void valueChanged(ListSelectionEvent e) {
    if (e.getSource() == jTable1.getSelectionModel()) {
      System.out.println("Event occurred in table 1");
    } else if (e.getSource() == jTable2.getSelectionModel()) {
      System.out.println("Event occurred in for table 2");
    }
}

OTHER TIPS

Register FocusListener to both tables to track which table was focused last.

More info can be found at http://docs.oracle.com/javase/tutorial/uiswing/events/focuslistener.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top