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.

  1. import javax.swing.*;
  2. public class HelloWorldSwing
  3. {
  4. private static void createAndShowGUI()
  5. {
  6. JFrame frame = new JFrame(“HelloWorldSwing”);
  7. // set up the window
  8. frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
  9. JLabel label = new JLabel(“Hello World”);
  10. // add label
  11. frame.add(label);
  12. frame.setSize(300,200);
  13. // display window
  14. frame.setVisible(true);
  15. }
  16. public static void main(String[] args)
  17. {
  18. javax.swing.SwingUtilities.invokeLater(new Runnable()
  19. {
  20. // schedule for the event dispatching thread:
  21. // creating, showing this app’s GUI.
  22. public void run()
  23. { createAndShowAPI(); }
  24. });
  25. }
  26. }

 

collections in JAVA

 

Key Methods:-

This section explains the key methods used in the early code. The method can be divided into two different categories.

  1. Methods for setting up the frame and adding a label.
    1. 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.

  1. 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.
  2. 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.
  3. 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:

  1. Executing the application code.

This task involves starting the GUI application and executing the code for rendering the GUI.

  1. 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.

  1. 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.

Written by Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Comment