advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

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.

 Microsoft RIA Development Center
 IBM Rational Resource Center
 Destination .NET
XML error: not well-formed (invalid token) at line 33
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Free VMware Server 2.0 Now Release Candidate
Linux Player Xandros Grabs Storied Rival Linspire
Hey Enterprise: Here Comes the 3G iPhone
MySpace Opens Profile Portability API
Microsoft Jumps Into Virtualization Fray
Eclipse Ganymede Makes It Easier for Devs
Open Source Nokia a Threat to Microsoft, Google?
Salesforce, Google Head for 2nd on Apps
HP Open Sources Unix File System for Linux
Red Hat Opens Its Network to Space

Build a Generic Histogram Generator for SQL Server
Beyond XML and JSON: YAML for Java Developers
Mastering the Windows Mobile Emulators
Avaya AE Services Provide Rapid Telephony Integration with Facebook
Featured Algorithm: Intel Threading Building Blocks: parallel_reduce
Getting Started with Windows Live Admin Center
Eight Key Practices for ASP.NET Deployment
Java ME User Interfaces: Do It with LWUIT!
Talking VPro: Transcript
Bringing Semantic Technology to the Enterprise

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES