Home www.python.org
Download Documentation
Home
Overview
License
Jython 2.0
Jython 2.1
Installing
JVM Compatibility
 
Applets
Demos
ButtonDemo
CheckboxDemo
ChoiceDemo
LabelDemo
ListDemo
CoordinatesDemo
ConvertDemo
 
Applet Problems
Here's what to do
Other applet issues
 
Email Us
jython-dev@lists.sourceforge.net
 
 
SourceForge Logo
  

Using Choices

This example shows how to use Choices from Jython.

Something has gone wrong loading this applet.

The complete source code for this example is included below.


from java import awt, applet class ChoiceDemo(applet.Applet): def init(self): self.choices = awt.Choice(itemStateChanged = self.change) for item in ['ichi', 'ni', 'san', 'yon']: self.choices.addItem(item) self.label = awt.Label() self.change() self.add(self.choices) self.add(self.label) def change(self, event=None): selection = self.choices.selectedIndex, self.choices.selectedItem self.label.text = 'Item #%d selected. Text = "%s".' % selection
The init method first creates a Choice object and sets its callback for when a new item is selected. This callback is specified as the itemStateChanged event property. Then four items are added to the choice object and the rest of the layout is initialized.

The change method is invoked whenever the selection in the choice object changes. It uses Python string formatting operator to display the current state to the user.