HTML Markup | JavaScript | Java | Home & Links

Tutorial 9 - Swing GUI Widgets

A Graphical User Interface (GUI) is a visual paradigm which allows users to communicate with programs in an intuitive way. Features include widgets (aka controls) and event driven activities. Users expect a graphical interface!

Java has two GUI packages, the original Abstract Windows Toolkit (AWT) and the newer Swing. AWT uses the native operating system's window routines so the visual effect is dependent on the run-time system platform. Swing allows three visual modes: a unified look and feel [the default], the native platform look, or a specific platform's look. Swing is built on the original objects and framework of AWT. Swing components have the prefix J to distinguish them from the original AWT ones (eg JFrame instead of Frame). To include GUI components and methods in your project you must import the java.awt.*, java.awt.event.* and javax.swing.* packages. SwingSet 3 demos the latest Swing widgets. A GUI reference with lots of examples can be found in the Guidebook. Intermediate|Advanced

Containers: Windows and Content Panes

Containers are components that are used to hold and group widgets such as text fields and checkboxes and other components. Methods common to many containers include: add(), pack(), requestFocus() and setToolTipText().

Windows are top-level containers which interface to the operating system's window manager. Their types are: Primary (adorned with minimize, maximize and close buttons) [JFrame]; Secondary [JDialog]; Plain (no border - used for splash screens) [JWindow] and XHTML document [JApplet].

JFrame is the most commonly used top-level container. It adds basic functionality such as minimize, maximize, close, title and border to basic frames and windows. Some important JFrame methods are: getTitle(), setBounds(x,y,w,h), setLocation(x,y), setMaximumSize(w,h), setMinimumSize(w,h), setPreferredSize(w,h), setResizable(bool), setSize(w,h), setTitle(str) and setVisible(bool). setDefaultCloseOperation(EXIT_ON_CLOSE) enables the close icon.

Content panes are lower level containers such as JPanel, JInternalFrame, JOptionPane, JLayeredPane, JScrollPane, JSplitPane and JTabbedPane which organize the layout structure when multiple controls are being used. JInternalFrame is an adorned lower-level frame used with MDI (Multiple Document Interface) to prevent document overlapping.

JPanel is the most commonly used content pane. An instance of the pane is created and then added to a frame. The add(widgetName) method allows widgets (ie GUI controls) to be added to the pane. layout managers control the way widgets are added. JPanel defaults to FlowLayout. Other content panes default to BorderLayout.

The following is a simple template that creates a JFrame container class using inheritance. The created subclass then adds a JPanel. This custom class will form the basis of many of our GUI examples.

Colors and Fonts

Colors can be selected from awt Color class constants or created (with transparency) using the constructor Color(red, green, blue, [alpha]) where the parameters are integer [0 - 255] or float [0.0 - 1.0]. An alpha level of 255 or 1.0 yields opaque [default]. For example: skyblue is 135,206,235 (ie. 87CEEB hex).

Background colors are set with setBackground(color_obj). Text colors are set with either setColor(color_obj) or the setForeGround(color_obj) depending on the object. Color settings are read using the integer methods: getRGB(), getRed(), getGreen(), getBlue() and getAlpha().

Font objects are created from Font class objects, eg Font bigFont=new Font("TimesRoman",Font.ITALIC,24);
Note that the parameters are family, style (as a Font class constant) and point size. Style is one of PLAIN, ITALIC or BOLD. Other objects can use the new font style by calling the setFont(fontObject) method.

FontMetrics defines a class that accesses the height information of a font in pixels. The class methods are: getLeading(), getAscent(), getDescent() and getHeight() [overall dimension].

Borders and Separators

Borders are used to separate or group widgets in a logical manner. They also 'dress up' the GUI. Border classes are imported from the javax.swing.border package. BevelBorder and SoftBevelBorder can be applied to buttons to give them a 3D look. EmptyBorder is used to 'pad' a widget with spacing. EtchedBorder and LineBorder separate widgets or panes for logical grouping. MatteBorder allows images or colors to be used to construct the border areas. TitledBorder allows adding a 'fieldset' frame and a 'legend' caption to an object such as a button group. An example of a titled border object is:

Border blackline=BorderFactory.createLineBorder(Color.black);
TitledBorder tb=new TitledBorder(border,"legend");

Any object can use the setBorder(border_obj) method to establish a border around it.

Line separators are used for visual separation of components in a panel. They are constructed with JSeparator(orient). Lines are oriented using either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.

Labels and Icons

Labels are non-interactive text objects most commonly used as prompts. They are created using the JLabel() constructor with the required text as the first parameter. Another parameter can be added using a SwingConstant value to set horizontal alignment. Vertical alignment is through the setVerticalAlignment() method. The contents of a label can be changed with the setText() method.

Icons can be easily added to labels or other controls either to brand, dress up, or aid accessibility. Icons are constructed from the ImageIcon class and then added as a parameter to the label (or other) control. An extra parameter can be used to control the position of the text relative to the icon. It must use one of the SwingConstants values.

ImageIcon icon=new ImageIcon("smile.gif");
JLabel label=new JLabel("hello",icon,SwingConstants.RIGHT);
pane.add(label);

Dialog Boxes

Dialogs are short messages, confirmations or input prompts for simple string information which appear as popup windows. Dialogs can either be modal (must be dismissed before another window is accessed) or modeless.

JOptionPane provides a modal dialog with predefined methods for each type of prompt. Each JOptionPane method has a first parameter that points to a parent (ie. window that it appears in) or null (default to the current window). The second parameter is the message or prompt to be displayed. New instances of JOptionPane are not normally generated.

showMessageDialog() has two more optional parameters to set a dialog title and to select the dialog's icon. The dialog has a single 'ok' button for completion and no data is returned by this method.

JOptionPane.showMessageDialog(null,"This is just a message",
    "Message Dialog",JOptionPane.PLAIN_MESSAGE);

showConfirmDialog() has three more optional parameters to set a dialog title, alter the button display, and select the dialogs icon. By default there are three buttons 'Yes', 'No' and Cancel for dialog completion. The returned value is one of JOptionPane.YES_OPTION, JOptionPane.NO_OPTION or JOptionPane.CANCEL_OPTION.

pressed=JOptionPane.showConfirmBox(null,"Everything aok");
if (pressed==JOptionPane.YES_OPTION) {/* do action for confirmed */}
if (pressed==JOptionPane.NO_OPTION) {/* do action for not confirmed */}
if (pressed==JOptionPane.CANCEL_OPTION) {/* do action for cancel op */}

showInputDialog() has two more optional parameters to set a dialog title and to select the dialog's icon. There is both an 'ok' button and a 'cancel' button for dialog completion. Any information typed into the entry box is returned as a string.

user_data=JOptionPane.showInputDialog(null,"What's your name");

The list of icon types that can be displayed (by predefined constant) contains ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE and WARNING_MESSAGE.

JDialog provides a simple unadorned window used to create customized dialog boxes. Customization can include modality and mnemonics (aka hotkeys).

Freeform Text Entry

Single line free form text fields are created using the JTextField() constructor. A string value is returned. JPassWordField() is similar to JTextField() but displays any typed characters as asterisks for security. Text fields can also be used to display output information!

Text areas are editing areas for blocks or multiple lines of text. They are constructed from JTextArea (simple text), JEditorPane (html, rtf and other formatted text) or JTextPane classes. Useful methods include getText(), setText(), append(str), insert(str,pos), replace(str,beg,end), setEditable(bool), setLineWrap(bool) and setWrapStyleWord(bool). Text areas are often contained within JScrollPane containers to allow scrolling of the display area. Download WSFile.java for an example of a handy class for moving text area data to/from files.

Formatted text fields allow display in a locale specific manner as well as restricting entry to valid characters. The class constructor used is JFormattedTextField(). Formats include: Date, Integer, Float, currency, percent and URI. Invalid input will be rejected. Up/down arrows cause some format data to alter by one. The escape key is used for canceling entries. Download SimpleFTF.java to see how formatted text fields are set up and used.

Basic Event Listeners

GUIs are event-based as they respond to user actions such as buttons, keyboard input and mouse activities. Java uses event listeners to monitor activity on specified objects and react to specific conditions. Check the appendix for a listing of event listeners. View advanced event listeners for techniques on organizing many events in larger projects.

The first step in adding a basic event handlers to widgets is to import awt.event.* which contains all of the event classes. Next add the phrase implements ***Listener (where *** is the appropriate event) to the class header to set up an interface. Register event listeners for a widget using the add***Listener(this) method. The reserved word this indicates that the required (by implements ***Listener) handler method called ***Performed() will be included in the current class.



JR's HomePage | Comments [jatutor9.htm:2014 05 12]