Search code examples
javaswingjtablelistselectionlistener

ListSelectionListener for many jtables


I have 2 questions for this
First question:
What's the best way (in terms of performance) to add a ListSelectionListner event to JTable.
This:

myTable.getSelectionModel().addListSelectionListener(this);

Or this:

myTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {

                //Do my stiff here...

                }
            }
        });

Second question:
I have this code:

myFirstTable.getSelectionModel().addListSelectionListener(this);
mySecondTable.getSelectionModel().addListSelectionListener(this);

How can I know the JTable that fired the ListSelectionListner event ?


Solution

  • Part 1

    Irrelevant, neither is better or worse from the perspective of performance, it will come down to needs. Obviously if you have a single listener added to multiple tables it would be more efficient from a memory point of view

    Which you would use would come down to needs and what you are trying to achieve

    Part 2

    Use the ListSelectionEvent#getSource method to determine what actually triggered the event