0% found this document useful (0 votes)
60 views23 pages

Swings

Swing was created to address limitations of the AWT by making components lightweight and customizable. Swing components are built on AWT but use lightweight peers instead of native peers. This allows components to be rendered the same on all platforms and for custom look and feels. A Swing GUI contains components like buttons and containers like panels that hold other components. The key containers are top-level containers like JFrame and lightweight containers like JPanel. Top-level containers define panes like the content pane components are added to.

Uploaded by

jeevitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views23 pages

Swings

Swing was created to address limitations of the AWT by making components lightweight and customizable. Swing components are built on AWT but use lightweight peers instead of native peers. This allows components to be rendered the same on all platforms and for custom look and feels. A Swing GUI contains components like buttons and containers like panels that hold other components. The key containers are top-level containers like JFrame and lightweight containers like JPanel. Top-level containers define panes like the content pane components are added to.

Uploaded by

jeevitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Introducing Swing

• The AWT defines a basic set of controls, windows,


and dialog boxes that support a usable, but
limited graphical interface.
• One reason for the limited nature of the AWT is
that it translates its various visual components
into their corresponding, platform-specific
equivalents, or peers.
• This means that the look and feel of a component
is defined by the platform, not by Java.
• Because the AWT components use native code
resources, they are referred to as heavyweight.
• The use of native peers led to several problems. First,
because of variations between operating systems, a
component might look, or even act, differently on different
platforms.
• This potential variability threatened the overarching
philosophy of Java: write once, run anywhere.
• Second, the look and feel of each component was fixed
(because it is defined by the platform) and could not be
(easily) changed.
• Third, the use of heavyweight components caused some
frustrating restrictions.
• For example, a heavyweight component is always
rectangular and opaque.
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 or slider.
• A container holds a group of components. Thus, a container
is a special type of component that is designed to hold
other components.
• Furthermore, in order for a component to be displayed, it
must be held within a container. Thus, all Swing GUIs will
have at least one container.
• Because containers are components, a container can also
hold other containers.
• This enables Swing to define what is called a containment
hierarchy, at the top of which must be a top-level container.
Components

• In general, Swing components are derived from


the JComponent class.
• JComponent provides the functionality that is
common to all components.
• JComponent inherits the AWT classes Container
and Component.
• Thus, a Swing component is built on and
compatible with an AWT component.
• All of Swing’s components are represented by
classes defined within the package javax.swing.
• The following table shows the class names for
Swing components
Containers

• Swing defines two types of containers. The first are top-level


containers: JFrame, JApplet, JWindow, and JDialog.
• These containers do not inherit JComponent. They do, however,
inherit the AWT classes Component and Container.
• Unlike Swing’s other components, which are lightweight, the top-
level containers are heavyweight.
• This makes the top-level containers a special case in the Swing
component library.
• As the name implies, a top-level container must be at the top of a
containment hierarchy.
• Furthermore, every containment hierarchy must begin with a top-
level container.
• The one most commonly used for applications is JFrame. The one
used for applets is JApplet.
• The second type of containers supported by
Swing are lightweight containers.
• Lightweight containers do inherit JComponent.
• An example of a lightweight container is JPanel,
which is a general-purpose container.
• Lightweight containers are often used to organize
and manage groups of related components
because a lightweight container can be contained
within another container.
• Thus, you can use lightweight containers such as
JPanel to create subgroups of related controls
that are contained within an outer container.
The Top-Level Container Panes
• Each top-level container defines a set of panes. At the top of the
hierarchy is an instance of JRootPane.
• JRootPane is a lightweight container whose purpose is to manage
the other panes. It also helps manage the optional menu bar.
• The panes that comprise the root pane are called the glass pane,
the content pane, and the layered pane.
• The glass pane is the top-level pane. It sits above and completely
covers all other panes.
• By default, it is a transparent instance of JPanel. The glass pane
enables you to manage mouse events that affect the entire
container (rather than an individual control) or to paint over any
other component, for example.
• In most cases, you won’t need to use the glass pane directly, but it
is there if you need it
• The layered pane is an instance of JLayeredPane.
• The layered pane allows components to be given
a depth value.
• This value determines which component overlays
another.
• The pane with which your application will interact
the most is the content pane, because this is the
pane to which you will add visual components.
• In other words, when you add a component, such
as a button, to a top-level container, you will add
it to the content pane.
The Swing Packages

• Swing is a very large subsystem and makes use


of many packages.
• The main package is javax.swing.
• This package must be imported into any
program that uses Swing.
• It contains the classes that implement the
basic Swing components, such as push
buttons, labels, and check boxes.
Create a Swing Applet

• The second type of program that commonly


uses Swing is the applet.
• Swing-based applets are similar to AWT-based
applets, but with an important difference: A
Swing applet extends JApplet rather than
Applet.
• JApplet is derived from Applet. Thus, JApplet
includes all of the functionality found in
Applet and adds support for Swing.
• Swing applets use the same four lifecycle
methods :
init( ), start( ), stop( ), and destroy( ).
• Of course, you need override only those
methods that are needed by your applet.
• One other point: All interaction with
components in a Swing applet must take place
on the event dispatching thread.
JLabel
• JLabel is Swing’s easiest-to-use component. JLabel can be
used to display text and/or an icon.
• It is a passive component in that it does not respond to
user input.
• JLabel defines several constructors. Here are three of them:
JLabel(Icon icon)
JLabel(String str)
JLabel(String str, Icon icon, int align)
• Here, str and icon are the text and icon used for the label.
The align argument specifies the horizontal alignment of
the text and/or icon within the dimensions of the label.
• It must be one of the following values: LEFT, RIGHT,
CENTER, LEADING, or TRAILING.
ImageIcon
• Icons are specified by objects of type Icon, which is an
interface defined by Swing.
• The easiest way to obtain an icon is to use the ImageIcon
class.
• ImageIcon implements Icon and encapsulates an image.
Thus, an object of type ImageIcon can be passed as an
argument to the Icon parameter of JLabel’s constructor.
• There are several ways to provide the image, including
reading it from a file or downloading it from a URL. Here is
the ImageIcon constructor used by the example in this
section:
ImageIcon(String filename)
• It obtains the image in the file named filename.
• The icon and text associated with the label can be
obtained by the following methods:
Icon getIcon( )
String getText( )
• The icon and text associated with a label can be
set by these methods:
void setIcon(Icon icon)
void setText(String str)
• Here, icon and str are the icon and text,
respectively.
• Therefore, using setText( ) it is possible to change
the text inside a label during program execution.
JTextField
• JTextField is the simplest Swing text component. JTextField allows
you to edit one line of text.
• It is derived from JTextComponent, which provides the basic
functionality common to Swing text components.
• JTextField uses the Document interface for its model.
• Three of JTextField’s constructors are shown here:
JTextField(int cols)
JTextField(String str, int cols)
JTextField(String str)
• Here, str is the string to be initially presented, and cols is the
number of columns in the text field. If no string is specified, the text
field is initially empty.
• If the number of columns is not specified, the text field is sized to fit
the specified string.
• JTextField generates events in response to user
interaction.
• For example, an ActionEvent is fired when the
user presses ENTER. A CaretEvent is fired each
time the caret (i.e., the cursor) changes position.
(CaretEvent is packaged in javax.swing.event.)
• In many cases, your program will not need to
handle these events.
• Instead, you will simply obtain the string
currently in the text field when it is needed.
• To obtain the text currently in the text field, call
getText( ).
The Swing Buttons
• Swing defines four types of buttons: JButton, JToggleButton, JCheckBox,
and JRadioButton.
• All are subclasses of the AbstractButton class, which extends
JComponent.
• AbstractButton contains many methods that allow you to control the
behavior of buttons.
• For example, you can define different icons that are displayed for the
button when it is disabled, pressed, or selected.
• The following methods set these icons:
void setDisabledIcon(Icon di)
void setPressedIcon(Icon pi)
void setSelectedIcon(Icon si)
void setRolloverIcon(Icon ri)
Here, di, pi, si, and ri are the icons to be used for the indicated
purpose.
• The text associated with a button can be read
and written via the following methods:
String getText( )
void setText(String str)
• Here, str is the text to be associated with the
button.
• The model used by all buttons is defined by
the ButtonModel interface.
• A button generates an action event when it is
pressed. Other events are possible.
JButton
• The JButton class provides the functionality of a push button.
• JButton allows an icon, a string, or both to be associated with the
push button.
• Three of its constructors are shown here:
JButton(Icon icon)
JButton(String str)
JButton(String str, Icon icon)
• Here, str and icon are the string and icon used for the button.
• When the button is pressed, an ActionEvent is generated.
• Using the ActionEvent object passed to the actionPerformed( )
method of the registered ActionListener, you can obtain the action
command string associated with the button.

You might also like