Tutorials : Building Easy Java GUIs with Thinlet, Part 1 :

More About the Combobox and List

The item selected in a combobox can be obtained in two ways:
  1. Use the getSelectedIndex(comboBox) method to get the index value of the selected item.
  2. Use the getInteger(comboBox, "selected") method to get the same value.
If you need the text value shown in the combobox, then use getString(comboBox, "text").

When working with items in comboboxes or lists, you often have two properties for each item: a text to display in the GUI and an "internal name," typically a unique key, for example to a database. If you use the name-attribute of the choice element, for example, <choice text="Africa" name="AFR" /></i>, then you may fetch the name like this:

Object item = getSelectedItem(comboBox);
String key = getString(item, "name");

In a list where more choices may be selected, you'd code:

Object[] items = getSelectedItems(listBox);
for (int j = 0; j < items.length; j++) {
  Object item = items[j];
  String key = getString(item, "name");
}

Setting Data in the GUI

Now you know how to retrieve data, and setting data is no more complex, as shown in Table 4.

Table 4. This table shows how data is set in the widgets.
Widget XML element Data is set by...
Text Field <textfield name="textField"/> setString(textField, "text", "some text")
Text Area <textarea name="textArea" wrap="true" columns="30" rows="2"/> setString(textArea, "text", "some text")
Button <button text="Go" action="showResults()" /> setString(button, "text", "some text")
Label <label text="Thinlet Widgets"/> setString(label, "text", "some text")
Check Box <checkbox name="checkbox"/> setBoolean(checkBox, "selected", true)
Radio Button <label text="choice a" />
<checkbox name="radioButton1"
          group="group" />
<label text="choice b" />
<checkbox name="radioButton2"
          group="group" />
setBoolean(radioButton1, "selected", false)
Drop Down with input field <combobox name="comboBox">
  <choice text="USA"/>
  <choice text="Canada"/>
</combobox>
setInteger(comboBox, "selected", 1)
and(!)
setString(comboBox, "text", "Canada")
Drop Down w/o input field <combobox name="comboBox2" editable="false">
  <choice text="Europe"/>
  <choice text="Asia"/>
  <choice text="Africa"/>
</combobox>
setInteger(comboBox, "selected", 1)
List <list name="listBox" selection="multiple">
  <item text="Denmark" selected="true" />
  <item text="Sweden"/>
  <item text="Norway"/>
</list>
setBoolean(item, "selected", true)
 

Events

As mentioned previously, you may link events defined in the configuration file to Java method. Table 5 shows a list of the events available for all the common widgets.

Table 5. The events available for commonly used widgets.
event init       focuslost focusgained action insert remove caret perform expand
collapse
widget
label x x x            
button x x x x          
checkbox x x x x          
togglebutton x x x x          
combobox x x x x          
textfield x x x x x x x x  
textarea x x x x x x x    
spinbox x x x x          
slider x x x x          
list x x x x       x  
table x x x x       x  
tree x x x x       x x
menuitem x x x x          

Table 6 shows a short description of the all the events. Other details are found on Thinlet Overview/Events.

Table 6. A complete list of all the events.
Event Description
init A method to invoke only once when the loading of the xml resource (including this component) is finished.
focuslost Invoked when a component loses the keyboard focus, thus it is no longer the focus owner.
focusgained Invoked when a component gains the keyboard focus, thus it is now the focus owner.
action Depends on the widget. Typically invoked after a mouse click (buttons, boxes etc.) 
insert Gives notification that there was an insert into the text (and possibly a portion has been removed too).
remove Gives notification that a portion of the text has been removed.
caret To track whenever the caret position has been changed.
perform Textfield: Invokes the given method if enter was pressed in an editable and enabled textfield. Others: Calls the method whenever a double-click event occurs.
expand
collapse
expand: Called when the tree expands a node.
collapse: Called whenever a node in the tree has been collapsed.

The method called may be given one of the parameters shown in Table 7.

Table 7. These parameters may be used as attributes to the method called.
parameter description type
thinlet the thinlet instance Thinlet
this the widget object Object
widget name another widget in the config file Object
item the component inside a combobox, list, tree or table Object
this/widget name/item.attribute the value of a widget's attribute Depends on attribute

Figure 5 , the example taken from the Thinlet Web site, demonstrates how to use events.


Figure 5. Using Events:
This example is taken from the Thinlet Web site.

Its configuration file looks like this:

<panel gap="4" top="4" left="4">
  <textfield name="number1" columns="4" />
  <label text="+" />
  <textfield name="number2" columns="4" />
  <button text="=" action="calculate(number1.text, number2.text, result)" />
  <textfield name="result" editable="false" />
</panel>

The calculate method has three parameters: the two first are of the type attribute, which gives you the text entered in the two text fields directly. The third parameter is of type widget name. When the button is pressed, the sum is calculated. However, if you use the insert events on the two textfields, the sum will be calculated immediately when a digit is entered in any field:

<panel gap="4" top="4" left="4">
  <textfield name="number1" columns="4" 
             insert="calculate(this.text, number2.text, result)"/>
  <label text="+" />
  <textfield name="number2" columns="4" 
             insert="calculate(number1.text, this.text, result)"/>
  <button text="=" />
  <textfield name="result" editable="false" />
</panel>

Since the insert event is connected to the number1 and number2 fields, it's possible to replace their names with the word "this."

Figure 6 shows the use of the item parameter. It's a simple list with three items.


Figure 6. The Item Parameter:
This is a simple list with three items.

Here's the configuration file:

<panel columns="1" gap="4" top="4" left="4" bottom="4" right="4">
  <textfield name="textField" width="170" height="20"/>

  <list name="listBox" selection="multiple"
        action="action(item, textField)" 
        perform="perform(item, textField)">
    <item name="DK" text="Denmark" />
    <item name="S" text="Sweden"/>
    <item name="N" text="Norway"/>
  </list>
</panel>

A single click on an item invokes the action-method and a double click invokes the perform-method. These methods will write the text of the selected item in the textfield at the top of the panel. To show you how to change the properties of the item, the following code sets a background color and a new font:

public void action(Object item, Object textField) {
  String s = getString(item, "text");
  setColor(item, "background", new Color(0, 255, 0));
  setFont(item, new Font(null, Font.BOLD, 20));
  setString(textField, "text", "action, item text = " + s);
}

public void perform(Object item, Object textField) {
  String s = getString(item, "text");
  setColor(item, "background", new Color(0, 255, 255));
  setFont(item, new Font(null, Font.ITALIC, 15));
  setString(textField, "text", "perform, item text = " + s);
}

The Color and Font classes are taken from the java.awt package. Figure 7 shows a selected Denmark and a double-clicked Sweden:


Figure 7. The Color and Font Classes:
These classes are taken from the java.awt package.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.