Home www.python.org
Download Documentation
Jython 2.0
Jython 2.1
JVM Compatibility
Applet Problems
Here's what to do
Other applet issues
Email Us
SourceForge Logo

Using Lists

This example shows how to use Lists from Jython.

Something has gone wrong loading this applet.

The complete source code for this example is included below.

from java import applet, awt from java.awt.event import ItemEvent from pawt import GridBag class ListDemo(applet.Applet): def fillList(self, list, names): list.actionPerformed=self.action list.itemStateChanged=self.change for name in names: list.add(name) def init(self): self.spanish = awt.List(4, 1) self.fillList(self.spanish, ['uno', 'dos', 'tres', 'cuatro', 'cinco', 'seis', 'siete']) self.italian = awt.List() self.fillList(self.italian, ['uno', 'due', 'tre', 'quattro', 'cinque', 'sei', 'sette']) self.output = awt.TextArea(10, 40, editable=0) bag = GridBag(self) bag.add(self.output, fill='BOTH', weightx=1.0, weighty=1.0, gridheight=2) bag.addRow(self.spanish, fill='VERTICAL') bag.addRow(self.italian, fill='VERTICAL') self.language = {self.spanish:'Spanish', self.italian:'Italian'} def action(self, e): list = e.source text = 'Action event occurred on "%s" in %s.\n' self.output.append(text % (list.selectedItem, self.language[list])) def change(self, e): list = e.source if e.stateChange == ItemEvent.SELECTED: select = 'Select' else: select = 'Deselect' text = '%s event occurred on item #%d (%s) in %s.\n' params = (select, e.item, list.getItem(e.item), self.language[list]) self.output.append(text % params)
The fillList method is defined to take a list and a set of names and insert those names as list items. It also sets the action methods for the list for both single and double clicking.

This init method creates and fills lists of spanish and italian numbers. It uses a GridBag to layout the lists and a text widget in the applet. This GridBag is a wrapper placed on the awt.GridBagLayout and GridBagConstraints classes to make this very powerful layout method easier to use.

The action method is invoked whener a list item is double clicked. It uses Python's % operator on strings for convenient text formatting.

The change method is invoked when a list item is selected or deselected. It's primary difference from the Java implementation is to use a dictionary of languages instead of a case statement to determine the appropriate text to display. This is an example of the ease of use of standard data types (lists and dictionaries) from Python.