[110918]: htdocs / applets / choice.ht  Maximize  Restore  History

Download this file

47 lines (34 with data), 1.4 kB

Title: Choice Example

<h3>Using Choices</h3>

<p>This example shows how to use Choices from Jython.

<applet code="ChoiceDemo" archive="appletdemo.jar" width = 350 height = 40
        alt="This browser doesn't support JDK 1.1 applets.">
<h2>Something has gone wrong loading this applet.</h2>

<p>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.label = awt.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
<a href="../docs/properties.html">event property</a>.  
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.


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks