Java-Swing

Swing

The Swing package in Java enables the programmers to create Graphical User Interface applications. It contains a number of classes that help to implement Windows-based tasks, such as manipulating windows, adding scroll bars, buttons, list items, text boxes, etc. All the classes are contained in the javax.swing package. These classes are hierarchically arranged inside the swing package in such a manner that each successive level in the hierarchy adds certain attributes to the Graphical User Interface application. Swing has been widely used by Java programmers. Swing did not exist in the early days of Java. Rather, it was a response to deficiencies present in Java’s original GUI subsystem: the Abstract Window Toolkit. The AWT defines a basic set of controls, windows, and dialog boxes that support a usable, but limited graphical interface.

Difference Between Swing and AWT

One of the main differences between Swing and AWT is that Swing will always generate similar type of output irrespective of the underlying platform. AWT on the other hand, is more dependent on the underlying operating system for generating the graphic components; thus the output may vary from one platform to another. Swings can be regarded as more graphically-rich than AWT not only because they provide some entirely new graphical components but also due to the fact that they have enhanced some of the conventional AWT components.

First Swing Application

Swing programs differ from both the console-based programs. The best way to understand the structure of a Swing program is to work through an example. The following program shows one way to write a Swing application. In the process, it demonstrates several key features of Swing. It uses two Swing components JFrame and JLabel. JFrame is the container that is commonly used for Swing applications. JLabel is the Swing component that creates a label, which is a component that displays information. The program uses a JFrame container to hold an instance of a JLabel. The label displays a short text message.

The program begins by importing javax.swing.JFrame and JLabel. This package contains the Swing components. It will be included in all programs that use Swing. Next, the main( ) is where most of the action of the program occurs. It begins by creating a JFrame frm that defines a rectangular window complete with a title bar; close, minimize, maximize, and restore buttons; and a system menu. The title of the window is passed to the constructor.

Next, the setSize( ) method sets the dimensions of the window, which are specified in pixels. The width of the window is set to 700 and the height is set to 200. The next line of code creates a JLabel component lbl. JLabel is used to displays information, which can consist of text, an icon, or a combination of the two. The JLabel created by the program contains only text, which is passed to its constructor.

The next line of code frm.add( lbl ) adds the label to the content pane of the frame. By default, the content pane associated with a JFrame uses border layout. This version of add( ) just shown adds the label to the left center location. The next line of code frm.setVisible(true) causes the window to become visible. If its argument is true, the window will be displayed. Otherwise, it will be hidden. By default, a JFrame is invisible, so setVisible(true) must be called to show it.

By default, when a window is closed, the window is removed from the screen, but the application is not terminated. While this default behavior is useful in some situations, it is not what is needed for most applications. Instead, you will usually want the entire application to terminate when its window is closed. There are a couple of ways to achieve this. The easiest way is to call setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ). After this call executes, closing the window causes the entire application to terminate.

Program

5

Program Source

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Javaapp {
    
    public static void main(String[] args) {
        
        JFrame frm = new JFrame("First GUI Application");
        frm.setSize(700, 200);
        
        JLabel lbl = new JLabel("Information");
        
        frm.add(lbl);
        frm.setVisible(true);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Leave a Comment