GUI Builder Makes Java GUI Development Easy

Java
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

If you’ve ever written an interactive Java program, then you know that designing the GUI is not exactly a straightforward process. Basically, you have two choices. You can code each component of the interface in a Java source file piece by piece, or you can use one of the visual development tools. No matter which technique you use, there’s a trade-off. Using the traditional method of coding every component means you need to have a thorough understanding of layout managers, containers, and action listeners, to name a few things. The traditional method is not easy. On the other hand, if you use an integrated development environment (IDE) such as VisualAge for Java, designing the GUI is very easy. But tying all the GUI components together into a working application is a lot more difficult. To successfully use one of these IDEs, you first need to have a thorough understanding of Java. If you ever have to go back and modify one of the Java source files generated by an IDE and you don’t have the IDE available, then you’ll quickly discover that IDEs are nothing more than code generators, and the code they generate is unduly complex and nearly incomprehensible. Manual modifications are a nightmare. There needs to be a way to combine the flexibility of coding the pieces you want to code by hand with a simple GUI designer that lets you quickly create a Java GUI. If the IDE could make the generated code easy to understand and modify, two-thirds of the battle for programming in Java would be over.

Evidently, IBM also felt that something like this was needed. It created a new Java GUI design tool, GUI Builder, which lets you very rapidly build reusable Java GUI panels. However, before you can appreciate the ease GUI Builder will bring to your programming life, you must first understand why writing interactive Java programs the conventional way is so tedious. Since developing Java apps with an IDE is a monumental undertaking in and of itself, I’ll skip any comparisons between that and the new GUI development tool. I’ll leave it to you to try the IDEs for yourself.

Traditional GUI Design

I’ll do something very simple. I’ll create the Java GUI panel shown in Figure 1. This is a simple application that can be called from any DOS command line. As you can see, the only components on this panel are a label, a text box, and a button. Pretty basic, huh? At least it seems that way until you start digging into the code that makes it work. I’ll show you what I mean. Figure 2 contains the code that displays the panel shown in Figure 1.


The Pieces

In Figure 2 (page 90), at Label A, I’ve told Java to define a new Canvas object named GUICanvas. GUICanvas is a separate Java class and does nothing more than extend the Java Canvas class interface. It is the area where the GUI components will be displayed, inside a standard Windows frame. Label B tells Java that I want to use the Gridbag layout manager to define how components will be displayed in the GUI panel. It’s up to you to decide which layout manager you’ll need to use. The C labels are used, in conjunction with the statement at Label B, to instantiate the Gridbag layout manager. Label D is used to instantiate the new canvas. The statements marked with Label E are used to define the GUI components, which, for this example, include a label, text field, and button. Each of these groups of instructions calls the addComponent method defined at Label H to actually add the components to the canvas. Because you have to specify the exact grid coordinates of each component in relation to all the other components, it’s easier to create one method to handle the placement and adding of the components to the canvas. Label F registers an actionListener to the button. This tells Java that some activity will be occurring with this GUI component and it should “listen” for this activity and take appropriate action. In this case, the action to be performed is to close the GUI panel. After all that, you’re finally ready to display the GUI panel, which is what happens at Label G.

Piece of cake, right? Probably not, unless you’ve been programming in Java for quite some time and are used to it. And this is a really simple Java GUI! Imagine if you had a complex GUI to design and had to lay it out manually on your canvas. Yeesh! This is one of the reasons Java is still on the back burner of acceptance by traditional AS/400 green- screen programmers. After all, once you’ve used a tool like Screen Design Aid on the AS/400 to create display files, you’re not going to be very accepting of something that makes you go back to graph paper and coordinate marking. Besides the drudgery of having to add all these components manually and defining the layout manager and canvas and on and on and on, all this stuff makes your code unnecessarily confusing. Instead of using this method, I will show you a tool that removes most of this complexity.

GUI Builder

GUI Builder is a Java-based WYSIWYG GUI design tool that was introduced with Modification 2 of the AS/400 Toolbox for Java. If you have Version 4, Release 4 of OS/400 or higher on your AS/400, then the Mod 2 version of the tool box resides there. I suggest, however, that you download the latest version of the AS/400 Toolbox for Java from the JTOpen site. (Go to www.as400.ibm.com/toolbox and follow the JTOpen links.)

GUI Builder is a visual design tool that lets you very rapidly and easily build Java GUI panels from scratch. It also lets you convert existing Windows Resource files (file type of RC) into an Extensible Markup Language (XML) representation that can be understood by GUI Builder. This lets you very quickly turn your existing Windows GUI definitions, those you created in C++ for example, into a format that can be understood by Java. In other words, in just a few minutes you can take a library of C++ GUI definitions you designed for Windows machines and make them completely platform-independent.

GUI Builder is able to accomplish all this by using a new technology developed by IBM called Panel Definition Markup Language (PDML). PDML is based on XML and is a way of describing GUI interfaces that are platform-independent. PDML, like XML or HTML, is a tag-based language. That is, each component of a GUI panel is defined with appropriate tags such as

PANEL1.LABEL1
22,60
101,15
PANEL1.LABEL1

PANEL1.TEXTFIELD1
138,61
94,22
SimpleGUI2Src SimpleGUI2Bean


Figure 4: The PDML file generated by GUI Builder is easy to understand for the example.

*


import com.ibm.as400.ui.framework.java.*;
import java.awt.Frame;
public class SimpleGUI2Src
{ public static void main(String[] args)

{

PanelManager pm = null;

try { pm = new PanelManager("SimpleGUI2", "PANEL1", null, new

Frame()); }

catch (DisplayManagerException e)

{

// Something didn't work, so display a message and exit

e.displayUserMessage(null);

System.exit(1);

}

// Display the panel

pm.setVisible(true);

// Exit the application

System.exit(0);

}

}

Figure 5: Contrast this Java code to that shown in Figure 1. Both display the same GUI.


BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$