Bober02
Bober02

Reputation: 15341

Java - listeners in Swing

I am starting to write some code in Java Swing and I am getting a little bit mixed up in the hierarchy of listeners that swing offers. Namely I wanted to know when to use, for instance, an ActionListener over ListSelectionListener and how to differentiate when a particular listener gets called from the UI i.e. after what user interaction.

Thanks very much for your responses.

Upvotes: 2

Views: 466

Answers (2)

Harmeet Singh
Harmeet Singh

Reputation: 2616

About ActionListener

An action event occurs, whenever an action is performed by the user. Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field. The result is that an actionPerformed message is sent to all action listeners that are registered on the relevant component. reference

About ListSelectionListener

List selection events occur when the selection in a list or table is either changing or has just changed. List selection events are fired from an object that implements the ListSelectionModel interface. To get a table's list selection model object, you can use either getSelectionModel method or getColumnModel().getSelectionModel(). reference

Q. how to differentiate when a particular listener gets called from the UI i.e. after what user interaction.

The above details and the references contains much information to get start with.

Upvotes: 2

Dahaka
Dahaka

Reputation: 517

ActionListener is used e.g. for JButton, it tells just that the GUI-element has done something (a button can not do anything except be clicked).

ListSelectionListener contains more information (which element has been choosen).

For more information on when to use which Listener check the javadoc of the element you want to use.

Upvotes: 0

Related Questions