Using Choices

This example shows how to use Choices from JPython.

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.