Java Swing-I

Home   »    Java   »    Advance Java   »    Java Swing-I


Swing, which is part of the Java Foundation Class (JFC) library, is an extension of the Abstract Window Toolkit (AWT). It offers much imporved functionality over its predecessor - new components, expanded components features, better event handling and drag and drop support.

Swing is a set of classes that provides more advanced and flexible components in comparison to Abstract Window Toolkit (AWT). AWT contains large number of classes and methods that allow you to create and manage Windows. Swing introduces new components such as tabbed panes, scroll panes, trees and tables and adds new features to the existing components. For example, you can associate both text string and image with the existing component, button, when the swing classes are used.

Swing components are platform independent whereas AWT components are not platform independent.

Swing Features

Swing development has its roots in the Model-View-Controller (MVC) architecture. The MVC- based architecture allows Swing components to be replaced with different data models and views. The pluggable look and feel is a result of the MVC architecture. Because Java is a platform-independent language and runs on any client machine, the look and feel of any platform has to be known.. The following is a summary of Swing's key features:

  1. Lightweight Compoents - Starting with the JDK 1.1, the AWT supports lightweight component development. For a compoenent to qualify as lightweight, it cannot depend on any native system classes. In Swing most of the components have their own view supported by Java look and feel classes.

  2. Pluggable Look and Feel - This feature enables the user to switch the look and feel of Swing components without restarting the application. The Swing library supports a cross-platform look and feel also called the Java look and feel that remains the same across all platforms wherever the program runs. The native look and feel is native to whatever particular system on which the program happens to be running, including Windows and Motif. The Swing library provides an API that gives flexibility in determining the look and feel of the applications.




Swing Components

Swing is a package built on top of the AWT that provides a great number of prebuilt classes (over 250 classes and 40 UI components). From a programmer's point of view, the UI components are probably the most interesting.

Swing CompoentsFeatures
JApplet An extended version of java.applet.Applet that adds support for root panes and other panes
JFrame An extended version of java.awt.Frame that adds support root panes and other panes
JPanel A generic lighweight container
JButton A push or command button
JLabel A display area for a short text string or an image (or both)
JTextField Allows the editing of a single line of text
JPasswordField Allows editing of a single line of text where the view does not show the original characters
JTextArea A multiple area that displays plain text
JCheckBox A checkbox that can be selected or deselected, displaying its state visually
JRadioButton A radio button that can be selected or deselected, displaying its state visually
JComboBox A combo box, which is a combination of a text field and dropdown list
JList A component that allows the user to select one or more objects from a list
JMenu A pop-up menu containing JMenuItem that's displayed when the user selects it in the JMenuBar component.
JMenuBar An implementation of a menu bar
JMenuItem An implementation of a menu item
JCheckMenuItem A menu item that can be selected or deselected and that displays its state visually
JRadioButtonMenuItem A radio button menu item
JPopupMenu A pop-up menu
JPopupMenuSeparator A pop-up menu-specific separator
JOptionPane Make it easy to pop up a standard dialog box
Input Dialog Boxes Make it easy to pop up a standard dialog box
JSlide A component that lets the user select a value by sliding a knob within an interval
JProgressBar A component that displays an integer value within an interval
JTable Presents data in a two-dimensional table format
JTabbedPane Lets the user switch between a group of components by clicking tabs
JFileChooser A JFileChooser object only presents the GUI for choosing files.
JRootPane A fundamental component in the container hierarchy
JScrollBar An implementation of a scrollbar
JScrollPane a container that message a viewport, optional vertical and horizontal scrollbars, and optional row and column heading viewports
JTextPane A text component that can marked up with attributes that are represented graphically
JToolBar A toolbar, useful for displaying commonly used controls
JTree Displays a set of hierarchical data as an outline
JSplitPane It manages two panes that are separated horizontally or vertically by a divider that you can reposition.
JDesktopPane It is display a container for one or more frames that can used to create a multiple-document interface and give a desktop feel to the application
JEditorPane It help display text and provide basic editing capabilities.

Note:

Each UI component begins with J, which is one of the reasongs so many programmers mistakenly use the terms JFC and Swing interchangeably.

One thing that must be noted in this list is that there's a Swing replacement for every AWT control and container except JCanvas; the reason is that the JPanel class already supports all that the Canvas component did, so Sun didn't find it necessary to add a separate JCanvas component.



Working With Swing

Using swing components are similar to using AWT components. Swing components are created by creating objects of the component class and adding the component object to a container. Swing is an extension to AWT conponents. Swing components are subclass of JComponent class.

JFrame

A Swing frame is a container that functions as the main window for programs that use Swing components. Swing frames possess a title, a border and buttons for iconifying, maximizing, and closing the frame.

Swing provides the JFrame class that is a part of the javax.swing package. The JFrame class is an abstract class. A Swing frame is represented by the class JFrame, that extends the AWT class Frame. The Swing frame is heavyweight container, as is its parent i.e. Frame class. An instance of JFrame can take the advantage of the features of both the JFrame and Frame class.

The JFrame class is subdivided into several different panes. The main pane is the content pane, which represents the full area of a frame in which components can be added.

The following steps are required to add component to a JFrame

Example

1. Create a JFrame object

The following creates a JFrame object.

    JFrame frm = new JFrame();

2. Get the content pane

    Container contPane =frm.getContentPane();

3. To add the compoent

    contPane.ad(someComponent);

The following table describes some of the methods provided by the JFrame class

MethodsDescription
void setContentPane(Container)
Container getContentPane()
Set or get the frame's content pane. The content pane contains the frame's visible GUI components and should be opaque.
JRootPane createRootPane()
void setRootPane(JRootPane)
JrootPane getRootPane()
Create, set, or get the frame's root pane. The root pane manages the interior of the frame including the content pane, the glass pane, and so on.
Void setGlassPane(Component)
Component getGlassPane()
Set or get the frame's glass pane. You can use the glass pane to intercept mouse events.
void setLayeredPane(JLayeredPane)
JLayeredPane getLayeredPane()
Set of get the frame's layered pane. You can use the frame's layered pane to put components on top of or behind other components.


Example:

The following creates JLabel object.



The statement creates a label Hello that is aligned to the center

The following example displays a label on a Frame.



C:\>jdk1.4\bin>javac MyFrame.java
C:\>jdk1.4\bin>java MyFrame

"Ouput of MyFrame.class"


Download Complete Program

  -  



JPanel

A JPanel is a Swing container (a component extension of JComponent) that is often used for grouping components within one area of an applet or a frame. A panel is also used to group other panels.

Swing panels are represented by the class JPanel, which is stored in the package javax.swing. JPanel is a lighweight component.

The following steps are required to add a component to the JPanel and then add the Panel to the Frame.

  1. Create a JFrame Object
  2.     JFrame frm=new JFrame();

  3. Create a JPanel object
  4.     JPanel pnl=new JPanel();

  5. Add all components (which can be containers) to the JPanel object by using its add() method.
  6.     pnl.add(<component name>);

  7. An intermediate JPanel object is made part of the content pane by calling the setContentPane() method of the JFrame class.
  8.     frm.setContentPane(pnl);

Example:

The following creates JButton object.



The statement creates a Button with the text "Save".

The following example displays six buttons on a panel using JFrame.



C:\>jdk1.4\bin>javac MasterForm.java
C:\>jdk1.4\bin>java MasterForm

"Output of MasterForm.class"


Download Complete Program

  -  




Java Swing-1


JFrame JPanel JApplet
JButtons JLabels JTextFields
JPasswordField JTextAreas JCheckBoxes
JRadioButtons JList JComboBox

<< Previous Topic
Next Topic >>



3 comments:

  1. Awesome! It's in fact awesome post, I have got much clear idea on the topic of java from this piece of writing.

    ReplyDelete
  2. Thank you for sharing this post, and very helpful for me.
    Java Training in OMR

    ReplyDelete
  3. I liked this article very much. The content is very good. Keep posting.
    Visit us: Java Training
    Visit us: Java Course

    ReplyDelete