Swing:
·
Swing is a set
of classes that provides more powerful and flexible GUI components than does
the AWT. Simply put, Swing provides the look and feel of the modern Java GUI.
·
The major
limitations of AWT are as follows:
o It translates its various visual components into
their corresponding, platform specific equivalents, or peers. This means that
the look and feel of the component is defined by the platform, not by Java.
o Because the AWT components use native code
resources, they are referred to as heavyweight.
o The use of native peers led to several problems:
§ Because of variations in between operating systems,
a component might look, or even act differently on different platforms.
§ The look and feel of each component was fixed and
not be (easily) changed.
§ The use of heavy weighted components caused
frustrating restrictions. For example, the heavy weight components are always
rectangular and opaque.
Swing
Feature:
The two key features of
Swing are as follows:0
·
Swing Components are Lightweight:
o Swing components are lightweighted that means they
are written entirely in Java and do not map directly to platform specific
peers.
o Because lightweight components are rendered using
graphics primitives, they can be transparent, which enables non-rectangular
shapes. Thus they are more efficient and more flexible.
o As lightweighted components do not translate into
native peers, the look and feel of each component is determined by Swing, not
by underlying operating system.
·
Swing Supports Pluggable Look and Feel:
o Swing supports a pluggable look and feel (PLAF).
Because each swing component is rendered by Java code rather than by native
peers, the look and feel of a component is under the control of swing.
o Swing separate the look and feel of a component from
the logic of the component that provides a significant advantage: it becomes
possible to change the way that a component is rendered without affecting any
of its other aspects. In other words, it is possible to “plug in” a new look
and feel for any given component without creating any side effects in the code
that uses that uses that component.
o It is possible to define entire set of look and feel
that represents different GUI styles. To use specific style, its look and feel
is simply “plugged in”.
o Pluggable look and feel provides several important
advantages:
§ It is possible to define look and feel that is
consistent across all platforms. Conversely, it is possible to create a look
and feel that acts like a specific platform.
§ The look and feel can be changed dynamically at run
time.
MVC
Connection:
·
In general, a
visual component consist of three distinct aspects:
o The way that the component looks when rendered on
the screen.
o The way that the component reacts to the user.
o The state information associated with the component.
·
The
Model-View-Controller (MVC) architecture is successful because each piece of
design corresponds to an aspect of a component.
·
In MVC
terminology,
o The model corresponds to the state information
associated with the component. For example, in case of a check box, the model
contains a field that indicates if the box is checked or unchecked.
o The view determines how the component is displayed
on the screen, including any aspects of the view that are affected by the
current state of the model.
o The controller determines how the component reacts
to the user. For example, when the user clicks a check box, the controller
reacts by changing the model to reflect the user’s choice(checked or unchecked)
·
By separating a
component into a model, a view, and a controller, the specific implementation
of each can be changed without affecting the other two.
·
Swing uses a
modified version of MVC that combines the view and the controller into a single
logical entity called the UI delegate. For this reason, Swing’s approach is
called either Model-delegate
architecture or the Separable Model
architecture.
·
Swing’s
pluggable look and feel made possible by its Model-Delegate architecture.
Because the view (look) and controller (feel) are separate from the model, the
look and feel can be changed without affecting how the component is used within
the program. Conversely, it is possible to customize the model without
affecting the way that the component appears on the screen or responds to user
input.
·
To support
model-delegate architecture, most Swing components contain two objects. The
first represents the model. The second represents the UI delegate. Models are
defined by interfaces. For example, the model for a button is defined by the
ButtonModel interface. UI delegates are classes that inherit ComponentUI. For
example, the UI delegate for button is ButtonUI.
Components
and Containers:
·
A Swing GUI
consists of two key items: components and containers.
·
A component is
an independent visual control, such as a push button and a slider.
·
A container
holds a group of components. Thus, a container is a special type of component
that is designed to hold other components. As containers are components, a
container can also hold other containers.
·
Component:
o Swing components are derived from JComponent class. JComponent provides
the functionality that is common to all components.
o JComponent inherits the AWT classes Container and Component.
Thus all Swing components are built on and compatible with an AWT component.
o All Swing components are represented by the classes
defined within the package javax.swing.
o All component classes in Swing starts with letter J.
The table below shows the list of classes’ names for Swing components (including
those used as containers).
JApplet
|
JButton
|
JCheckBox
|
JCheckBoxMenuItem
|
JColorChooser
|
JComboBox
|
JComponent
|
JDesktopPane
|
JDialog
|
JEditorPane
|
JFileChooser
|
JFormattedTextField
|
JFrame
|
JInternalFrame
|
JLabel
|
JLayeredPane
|
JList
|
JMenu
|
JMenuBar
|
JMenuItem
|
JOptionPane
|
JPanel
|
JPasswordField
|
JPopupMenu
|
JProgressBar
|
JRadioButton
|
JRadioButtonMenuItem
|
JRootPane
|
JScrollBar
|
JScrollPane
|
JSeparator
|
JSlider
|
JSpinner
|
JSplitPane
|
JTabbedPane
|
JTable
|
JTextArea
|
JTextField
|
JTextPane
|
JTogglebutton
|
JToolBar
|
JToolTip
|
JTree
|
JViewport
|
JWindow
|
No comments:
Write comments