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 :

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 widget—if 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.
What will you do? Link
See some demos thinlet.sourceforge.net/drafts.html
Learn about the widgets thinlet.sourceforge.net/component.html
Learn how to program in Thinlet thinlet.sourceforge.net/thinlet.html
How to set and get widget properties thinlet.sourceforge.net/properties.html
Learn about events thinlet.sourceforge.net/events.html
Use the newsgroup/forum groups.yahoo.com/group/thinlet/

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.

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.

 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 43
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%.

Google Hopes Chrome Will Help, Not Hurt Firefox
Remember Figlets? They're Back With Zend
Microsoft Readies an App Store Competitor?
Google: Chrome Browser Will Make Money
Sam Ramji: Microsoft's Man in Open Source
Google to Shake Up Browsers With Own Launch
Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel
Jim Zemlin: The New Center of Linux Gravity

Code Around C#'s Using Statement to Release Unmanaged Resources
Writing Functional Code with RDFa
BitLocker Brings Encryption to Windows Server 2008
Network Know-How: Exploring Network Algorithms
Create a Durable and Reliable WCF Service with MSMQ 4.0
The Baker's Dozen: 13 Tips for SQL Server 2008 and SSRS 2008
Book Excerpt: Microsoft Expression Blend Unleashed
Develop a Mobile RSS Feed the Easy Way
State of the Semantic Web: Know Where to Look
A 3D Exploration of the HTML Canvas Element

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
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
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
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES