An Introduction to Java GUI Programming

An article to launch the beginner into the world of Java GUI programming

Introduction

This article is meant for the individual who has little or no experience in Java GUI programming. As such, this paper will focus on the hierarchal tree structure that roots at the frame and goes into the content pane panel. The primary focus will then be on the button widget (control in .NET) and the corresponding method used to handle that event listener. Any .NET programmer will find these concepts extremely similar, except that the coding style requires more text is some cases and the terms used are different. A quick and easy way to compile this code on the command line after installing Sun's Java Runtime at www.java.com and Sun's J2EE SDK 5.0 is to go to the default directory: c:\Sun\SDK\JDK\bin> type con > somecode.java Ctrl-Z and then compile. To set your path:set PATH=%PATH%;.;C:\Sun\SDK\JDK\bin.

Java GUI programming involves two packages: the original abstract windows kit (AWT) and the newer Swing toolkit. Swing components have the prefix J to distinguish them from the original AWT ones (e.g. JFrame instead of Frame). To include Swing components and methods in your project, you must import the java.awt.*, java.awt.event.*, and javax.swing.* packages. Displayable frames are top-level containers such as JFrame, JWindows, JDialog, and JApplet, which interface with the operating system's window manager. Non-displaying content panes are intermediate containers such as JPanel, JOptionsPane, JScrollPane, and JSplitPane. Containers are therefore widgets or GUI controls that are used to hold and group other widgets such as text boxes, check boxes, radio buttons, et al. In .NET the main UI, called the Windows Form, holds the controls that are dragged and dropped onto the control surface. Every GUI starts with a window meant to display things. In Swing, there are three types of windows: the Applet, the Dialog, and the Frame. These interface with the windows manager. In swing, a frame object is called a JFrame. A JFrame is considered the top most container. These are also called displayable frames. Non-displaying content panes are intermediate containers such as JPanel, JScrollPane, JLayeredPane, JSplitPane and JTabbedPane which organize the layout structure when multiple controls are being used. Stated simply, the content pane is where we place out text fields are other widgets, so to add and display GUI controls, we need to specify that it is the content pane that we are adding to. The content pane is then at the top of a containment hierarchy, in which this tree-like hierarchy has a top-level container (in our case JFrame). Working down the tree, we would find other top level containers like JPanel to hold the components. Here is the code that produces a simple frame upon to build on:

If you have never compiled Java code, then consider this basic code in order to show the compilation and interpretation process. As .NET compilers emit IL code and metadata, where the metadata tables are read by the CLR to verify type safety (that is, that the correct data types are passed to the correct methods), the JIT compiler converts the IL code into native code for execution. There is no interpretation as there is with the Java Virtual Machine. The Java platform is defined by the APIs (collections of compiled libraries for use programs and the JVM (which is similar to the CLR). A Java source code file is compiled into byte code wherein a class file is generated that functions as a blueprint for the runtime execution. Here is an example:

import java.util.*;
publicclass Sys {
publicstaticvoid main(String[] args) {
System.out.println
(“This is a string passed to the print line method of the System class”);
}
}

The first step in adding a basic button push event handler to the above example is to import awt.event.* which contains all of the event classes. Next add the phrase implements ActionListener to the class header to use the interface. Register event listeners for each button widget using the addActionListener(this) method. The reserved word this indicates that the required (by implements ActionListener) handler method called actionPerformed() will be included in the current class. For example, consider this more colorful example:

Share

About the Author

I started electronics training at age 33. I began studying microprocessor technology in an RF communications oriented program. I am 43 years old now. I have studied C code, opcode (mainly x86 and AT+T) for around 3 years in order to learn how to recognize viral code and the use of procedural languages. I am currently learning C# and the other virtual runtime system languages. I guess I started with the egg rather than the chicken. My past work would indicate that my primary strength is in applied mathematics.