Now we want to do something with our list—that is, handle the list events—so we need to inspect JList's event properties (or look them up in the Java API documentation).
Here's the interactive session to inspect the list properties, with the output formatted to make it a little more readable.
[<beanEventProperty valueChanged for event interface javax.swing.event
We can inspect the events more closely using getEvent sInfo to print and pause.
>>> getEventsInfo(JList, 1, 1) Event Property: valueChanged
Defined in: javax.swing.event.ListSelectionListener
Event: j avax.swing.event.ListSelecti onEvent
Event properties for javax.swing.event.ListSelectionEvent:
lastIndex Type: org.python.core.PyBeanProperty valueIsAdjusting Type: org.python.core.PyBeanProperty firstIndex Type: org.python.core.PyBeanProperty
If you look up the list properties in the Java API documentation, taf se are taf ones you'll find:
• LastIndex— last row that may have changed
• ValuelsAdj usting— true if this is multiple change events
• Firstlndex— first row that may have changed
ListSelectionEvent allows handling of an item selection event, either the Java way or the Python way. Here's the Python way:
>>> def eventListener(event):
... list = event.source # get a reference to the list,
... index = event.firstlndex
... print list.model.getElementAt(index)
I'm going to leave the Java way as an exercise. I'll give you two hints to help you, but don't read them unless you get stuck. Hintl: Create a class that subclasses j ava. awt .event. ItemListener; then create an instance of that class, and pass it to the list. addltemListener () method. Hint2: The subclass should override the itemStateChanged () method with the arguments self and event.
Was this article helpful?