|
Finding the Widgets
You probably usually pass either the widgets or their properties in the method call defined in the configuration file. However, a Thinlet program may also locate any widgetif its name is known. In the above example, the textField widget passes as the second parameter, but you could leave it out and let the action and perform methods locate the widget directly:
public void perform(Object item) {
String s = getString(item, "text");
setColor(item, "background", new Color(0, 255, 255));
setFont(item, new Font(null, Font.ITALIC, 15));
// Locate the textField widget
Object tf = find("textField");
setString(tf, "text", "perform, item text = " + s);
}
In this situation, specifying the widgets as parameters prevents widget name binding between the program and the configuration file. When many widgets are involved in an action, it's nice to avoid long parameter lists, and let the code look up the widgets as needed.
Adding, Removing, and Disabling Widgets
Sometimes, the number and types of widgets depend on who's using the application, or what data is fetched from a database. This poses no problem for Thinlet, since widgets may be added from your Java program. Use the create and add methods to create the widget you'd like to add, and then add it to a "container" (typically your panel):.
<panel init="load(this)">
</panel>
Use the init event to call method load:
public void load(Object panel) {
Object label1 = create("label");
setString(label1, "text", "label 1");
add(panel, label1);
Object field1 = create("textfield");
setString(field1, "text", "field 1");
add(panel, field1);
}
Figure 8
shows the GUI from this setup:

Figure 8. Widget Management:
Use the create and add methods to add widgets.
All widgets in a panel are numbered starting from 0. To add a widget, find the place in the panel where your new widget should go and then enter the number of the widget immediately before it. To insert a checkbox between the label and the textfield:
Object checkbox1 = create("checkbox");
add(panel, checkbox1, 1);
To remove a widget, you need to have a reference to the widget. If you know its position in the GUI, you can do the following:
// Remove a widget:
Object box = getItem(panel, 1);
remove(box);
Instead of removing a widget, you can also disable it or make it invisible:
// Disable a widget:
setBoolean(checkbox1, "enabled", false);
// Making a widget invisible:
setBoolean(checkbox1, "visible", false);
Using Weights
If the widgets in a panel do not fill all the space available, use the weightx and weighty properties to stretch them to fill the available space:
<panel columns="2" gap="4" top="4" left="4" bottom="4" right="4">
<textfield name="textField1"/>
<textfield name="textField2"/>
<textfield name="textField3"/>
<textfield name="textField4"/>
<textfield name="textField5"/>
<textfield name="textField6"/>
</panel>
If you define the panel size (to the FrameLauncher) to 250 x 100, you'll get a GUI like in Figure 9
.

Figure 9. Using Weights:
Using the weigthx and weighty properties allows you to stretch widgets to fill the available space.
Now, you can stretch column one horizontally to fill the remaining space:
<panel columns="2" gap="4" top="4" left="4" bottom="4" right="4">
<textfield name="textField1" weightx="1"/>
<textfield name="textField2"/>
<textfield name="textField3"/>
<textfield name="textField4"/>
<textfield name="textField5"/>
<textfield name="textField6"/>
</panel>
Stretching vertically is, of course, also possible. Choose any field in row 1 to stretch row 1:
<panel columns="2" gap="4" top="4" left="4" bottom="4" right="4">
<textfield name="textField1" weightx="1"/>
<textfield name="textField2" weighty="1"/>
<textfield name="textField3"/>
<textfield name="textField4"/>
<textfield name="textField5"/>
<textfield name="textField6"/>
</panel>
To give all text fields the same size, give them all weights equal 1. You can also use values greater than 1, to prioritize the use of remaining space:
<panel columns="2" gap="4" top="4" left="4" bottom="4" right="4">
<textfield name="textField1" weightx="1"/>
<textfield name="textField2" weighty="1"/>
<textfield name="textField3"/>
<textfield name="textField4" weighty="2"/>
<textfield name="textField5"/>
<textfield name="textField6" weighty="3"/>
</panel>
Nested Panels
As you have seen, a single panel makes it possible to place widgets in a table-like layout. When you start to build more complex GUIs, you realize that this is somewhat restrictive. So Thinlet allows nested panels:
<panel columns="2" gap="4" top="4" left="4" bottom="4" right="4">
<textfield name="textField1" text="1"/>
<textfield name="textField2" text="2"/>
<panel columns="1">
<textfield name="textField3" text="3"/>
<textfield name="textField4" text="4"/>
</panel>
<panel>
<textfield name="textField5" text="5" weighty="1"/>
<textfield name="textField6" text="6"/>
</panel>
</panel>
The outer panel sets up a 2x2 table (columns="2"). Row 2, column 1 defines a new panel with two rows (because of columns="1"), and row 2, column 2 defines a panel with two columns:

Figure 10. Nested Panels:
Because single panels can be restrictive, Thinlet allows nested panels.
In most cases, nested panels and proper use of weightx and weighty (and rowspan/colspan) will give you what you want, but a few times I've experienced problems with the layout. There might be a few bugs in the layout engine.
The Thinlet Documentation
Most of the Thinlet documentation comes with the download. It's very condensed, so it's not very good for the beginner, but it's fine for the user who has some experience. When you download the tool, you'll also get the documentation, which is practically the same as what you'd find on thinlet.sourceforge.net. Table 8 is a quick guide to the documentation.
Table 8. A quick guide to Thinlet Documentation.
Get Ready For Part Two!
If you're eager to learn more about Thinlet, you have only to click here for Part 2, which will cover features like menus, dialogs, tabbed panes, trees, and will finish by presenting a couple of the GUI editors for Thinlet: Thing and Theodore.
Other Resources
All the programs and files from the article are in this zip-file.
The home of Thinlet: thinlet.sourceforge.net.
The Thinlet blog: thinlet.blog-city.com. Here you'll find links to other useful information, like news, examples, and applications on the Web.
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.
|