Java GUI’s Using the Swing API 2

Swing Packages:-

The Swing API has a rich and convenient set of packages that makes it powerful and flexible. The given below lists each package name and the purpose of each package.

1.     javax.swing:- Provides a set of light weight components such as, JFrame, JButton, JCheckBox and much more.

2.     javax.swing.border:- Provides classes and interfaces for drawing specialized border such as line, etched, bevel, matte and more.

3.     javax.swing .event:- Provides support for events fired by Swing components.

4.     javax.swing.undo:- Allows developers to provide support for undo/redo in applications such as text editor.

5.     javax.swing.colorchooser:- Contains classes and interfaces used by the JColorChooser component.

6.     javax.swing.filechooser:- Contains classes and interfaces used by the JFileChooser component.

7.     javax.swing.table:- Provides classes and interfaces for handling JTable.

8.     javax.swing.tree:- Provides classes and interfaces for handling JTree.

9.     javax.swing.plaf:- Provide one interface and many abstract classes that Swing uses to provide its pluggable look and feel capabilities.

10.                        javax.swing.plaf.basic:- Provide user interface objects built according to the basic look and feel.

11.                        javax.swing.plaf.metal:- Provide user interface objects built according to the Java look and feel.

12.                        javax.swing.plaf.multi:- Provide user interface objects that combine two or more look and feels.

13.                        javax.swing.plaf.synth:- Provide user interface objects for skinnable look and feel in which all painting is delegated.

14.                        javax.swing.text:- Provides classes and interfaces that deals with editable and non editable text components.

15.                        javax.swing.text.html:- Provide the class HTMLEditorKit and supporting classes for creating HTML text editors.

16.                        javax.swing.text.html.parser:- Provides the default HTML parser along with support classes.

Examining a Composition of a JAVA Technology GUI:-

A Swing API based GUI is composed of the following elements:

Containers are on the top of the GUI containment hierarchy. All the components in the GUI are added to these containers. JFrame, JDialog, JWindow, and JApplet are the top level containers.

All the GUI components are derived from the JComponent class, for example, JComboBox, JAbstractButton, and JTextComponent.

Layout Manager:-
Layout Managers are responsible for laying out components in a container. BorderLayout, FlowLayout, GridLayout are a few examples of the layout managers. There are more sophisticated and complex layout managers that give more control over the GUI.
java I/O Fundamentals
Swing Containers:-
The Swing containers can be classified in to three main categories:
Top level containers
General purpose containers
Special purpose containers

Top level Containers:-
Top level containers are at the top of the Swing containment hierarchy. There are three top level Swing containers: JFrame, JWindow, and JDialog.
There is also a special class JApplet which, while not strictly a top level container, is worth mentioning here because it should be used as the top level of any applet that uses Swing components.
The JApplet, Jframe, JDialog, and JWindow classes are directly derived from Applet, Frame, Dialog and Window classes, respectively.
In this, it is important to note this, because all the other Swing containers and components are derived from JComponents.

Swing Components:-
Swing GUIs use two kinds of classes: GUI classes and non GUI support classes.
The GUI classes are visual and descendants of JComponents, and are called J classes.
The non GUI classes provide services and perform vital functions for GUI classes; however they do not produce any visual output.
Swing components primarily provide components for text handling, buttons labels, lists, pans, combo boxes, scroll bars, scroll pans, menus, tables, and trees.
Swing components can be broadly classified as follows:
Text components
Uneditable information display components
Formatted display components
Other basic components

Properties of Swing Components:-
In this section, it describes the properties of Swing components.

Common Component Properties:-
All the Swing components share some common properties because they all extend the JComponent class.
The given below shows a subset of the common properties that all Swing components inherit from JComponent.

Property                                           Methods
Border                                                            Border getBorder()
void setBorder(Border b)
Background and Foreground color   void setBackground(Color bg)
void setForeground(Color bg)
Font                                                                 void setFont(Font f)
Opaque                                                           void setOpaque(boolean isOpaque)
Maximum and minimum size               void setMaximumSize(Dimension d)
void setMinimumSize(Dimension d)
Alignment                                                     void setAlignmentX(float ax)
void setAlignmentY(float ay)
Preferred size                                              void setPrefferedSize(Dimension ps)

Note:-Some properties, such as preferred size, are hints to the layout manager. Although a component can provide the layout manager with hints, the layout manager can ignore these hints and use other information to rendering the component.
Component Specific Properties:-
In this section, it discusses component properties specifically taking JComboBox as an example.
JComboBox inherits all the properties in the JComponent and defines more specific properties.
The given below shows some properties specific to JComboBox.

Properties                                Methods
Maximum row count               void setMaximumRowCount(int count)
Model                                             void setModel(ComboBoxModel cbm)
Selected index                             int getSelectedIndex()
Selected Item                              Object getSelectedItem()
Item count                                    int getItemCount()
Renderer                                       void setRenderer(ListCellRenderer ar)
Editable                                         void setEditable(boolean flag)

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.

Leave a Reply