Java GUI’s Using the Swing API 4
GUI Construction:-
A Java technology GUI can be created using either of the following technique:
- Programming construction
This technique uses code to create the GUI. This technique is useful for learning GUI construction. However, it is very laborious for use in production environments.
- Construction using a GUI builder tool
This technique uses a GUI builder tool to create the GUI. The GUI developer uses a visual approach to drag and drop containers and components to a work area. The tool permits the positioning and resizing of containers and components using a pointer device such as a computer mouse. With each step, the tool automatically generates the Java technology classes required to reproduce the GUI.
Programmatic Construction:-
This section describes creating a simple GUI that prints Hell World. The code shown below creates a container JFrame with a title HelloWorldSwing. It later adds a KLabel with the Accessible Name property set to Hello World.
- import javax.swing.*;
- public class HelloWorldSwing
- {
- private static void createAndShowGUI()
- {
- JFrame frame = new JFrame(“HelloWorldSwing”);
- // set up the window
- frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
- JLabel label = new JLabel(“Hello World”);
- // add label
- frame.add(label);
- frame.setSize(300,200);
- // display window
- frame.setVisible(true);
- }
- public static void main(String[] args)
- {
- javax.swing.SwingUtilities.invokeLater(new Runnable()
- {
- // schedule for the event dispatching thread:
- // creating, showing this app’s GUI.
- public void run()
- { createAndShowAPI(); }
- });
- }
- }
Key Methods:-
This section explains the key methods used in the early code. The method can be divided into two different categories.
- Methods for setting up the frame and adding a label.
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE): This method defines the behavior of the JFrame when a close operation is initiated. There are four possible ways of handling this.
1) DO_NOTHING_ON_CLOSE: Does nothing when the close operation is initiated. This constant is defined in WindowsConstants.
2) HIDE_ON_CLOSE: Invokes any WindowListener objects and hides the frame. This constant is defined in WindowConstants.
3) DISPOSE_ON_CLOSE: Invokes any WindowListener objects and hides and disposes the frame. This constant is defined in WindowConstants.
4) EXIT_ON_CLOSE: Exits the application. This constant is defined in the JFrame class.
- setVisible(true): When the JFrame is first created, it creates an invisible frame. To make the frame visible, the parameter for the setVisible should be set to true. JFrame inherits this method from the java.awt.Component.
- add(Component c): This method adds the components to the container. JFrame inherits this method from java.awt.Component class. Five different overloaded methods are defined in java.awt.Component.
- Methods for making the GUI thread safe and efficient.
Several tasks are involved in displaying the GUI efficiently. These tasks can be broadly defined as:
- Executing the application code.
This task involves starting the GUI application and executing the code for rendering the GUI.
- Handling the events raised from the GUI:
Several events raised by the components in the GUI. For example, when a button is pressed an event is generated. Event listeners should be defined to handle this event. This task dispatches the event to the appropriate listeners that handle the event.
- Handle some time consuming process:
Several activities might be time consuming and they can be run in the background so that the GUI would be efficient and responsive. These kinds of activities are handled by this task.
To handle these tasks efficiently, the swing framework uses threads that are light weight processes. The tasks described above can be handled by these threads separately and concurrently. The programmer should utilize these threads. The Swing framework provides a collection of utility methods in the SwingUtilities class.
- SwingUtilities.invokeLater(new Runnable());
In the Java programming language, threads are created using the Runnable interface. This interface defines a method run that should be implemented by all the classes using this interface. The invokeLater method schedules the GUI creation task to execute the run method asynchronously by the event handling thread after all the pending events are completed.