Upload
alisha-korpal
View
851
Download
0
Embed Size (px)
Citation preview
Internet
And
Java
Topic: - Java Swings
Submitted To: Submitted By:
As Prof. Shruti Jain Alisha Korpal
1 | P a g e
Nivia Jain
Sharuti Jain
2 | P a g e
Index
S.no Topic Page no.1 Introduction 32 History 43 Swing Architecture 54 Foundation 6-75 Relationship to AWT 86 Swing Components 97 JFrame 108 JLabel 119 JButton 1210 Example 1311 References 15
3 | P a g e
Introduction
Swing is the primary Java GUI widget toolkit. It is part of Java Foundation
Classes (JFC) — an API for providing a graphical user interface (GUI) for Java
programs.
Swing was developed to provide a more sophisticated set of GUI components than the
earlier Abstract Window Toolkit. Swing provides a native look and feel that emulates the
look and feel of several platforms, and also supports a pluggable look and feel that
allows applications to have a look and feel unrelated to the underlying platform. It has
more powerful and flexible components than AWT. In addition to familiar components
such as buttons, check box and labels, Swing provides several advanced components
such as tabbed panel, scroll panes, trees, tables and lists.
Unlike AWT components, Swing components are not implemented by platform-specific
code. Instead they are written entirely in Java and therefore are platform-independent.
The term "lightweight" is used to describe such an element.
4 | P a g e
History
The Internet Foundation Classes (IFC) was a graphics library for Java originally
developed by Netscape Communications Corporation and first released on December
16, 1996.
On April 2, 1997, Sun Microsystems and Netscape Communications
Corporation announced their intention to incorporate IFC with other technologies to form
the Java Foundation Classes. The "Java Foundation Classes" were later renamed
"Swing".
Swing introduced a mechanism that allowed the look and feel of every component in an
application to be altered without making substantial changes to the application code.
The introduction of support for a pluggable look and feel allows Swing components to
emulate the appearance of native components while still retaining the benefits of
platform independence. This feature also makes it easy to make an application written
in Swing look very different from native programs if desired.
Originally distributed as a separately downloadable library, Swing has been included as
part of the Java Standard Edition since release 1.2. The Swing classes and components
are contained in the javax.swing package hierarchy.
5 | P a g e
Swing Architecture
Foundations
o Extensible
o Customizable
o Configurable
o Light weight UI
Relationship to AWT
6 | P a g e
Foundations
Swing is platform independent both in terms of expression (Java) and implementation
(Look-and-Feel).
Extensible
Swing is a highly partitioned architecture, which allows for the "plugging" of various
custom implementations of specified framework interfaces: Users can provide their own
custom implementation(s) of these components to override the default implementations.
In general, Swing users can extend the framework by extending existing (framework)
classes and/or providing alternative implementations of core components.
Customizable
Given the programmatic rendering model of the Swing framework, fine control over the
details of rendering of a component is possible in Swing. As a general pattern, the
visual representation of a Swing component is a composition of a standard set of
elements, such as a "border", "inset", decorations, etc. Typically, users will
programmatically customize a standard Swing component (such as a JTable) by
assigning specific Borders, Colors, Backgrounds, opacities, etc., as the properties of
that component. The core component will then use these properties (settings) to
determine the appropriate renderers to use in painting its various aspects. However, it is
also completely possible to create unique GUI controls with highly customized visual
representation.
7 | P a g e
Configurable
Swing's heavy reliance on runtime mechanisms and indirect composition patterns
allows it to respond at runtime to fundamental changes in its settings. For example, a
Swing-based application can change its look and feel at runtime. Further, users can
provide their own look and feel implementation, which allows for uniform changes in the
look and feel of existing Swing applications without any programmatic change to the
application code.
Light weight UI
Swing's configurability is a result of a choice not to use the native host OS's GUI
controls for displaying itself. Swing "paints" its controls programmatically through the
use of Java 2D APIs, rather than calling into a native user interface toolkit. Thus, a
Swing component does not have a corresponding native OS GUI component, and is
free to render itself in any way that is possible with the underlying graphics APIs.
However, at its core every Swing component relies on an AWT container, since
(Swing's) JComponent extends (AWT's) Container. This allows Swing to plug into the
host OS's GUI management framework, including the crucial device/screen mappings
and user interactions, such as key presses or mouse movements. Swing simply
"transposes" its own (OS agnostic) semantics over the underlying (OS specific)
components. So, for example, every Swing component paints its rendition on the
graphic device in response to a call to component.paint(), which is defined in (AWT)
Container. But unlike AWT components, which delegated the painting to their OS-native
"heavyweight" widget, Swing components are responsible for their own rendering.
8 | P a g e
Relationship to AWT
Since early versions of Java, a portion of the Abstract Window Toolkit (AWT) has
provided platform-independent APIs for user interface components. In AWT, each
component is rendered and controlled by a native peer component specific to the
underlying windowing system.
By contrast, Swing components are often described as lightweight because they do not
require allocation of native resources in the operating system's windowing toolkit. The
AWT components are referred to as heavyweight components.
Much of the Swing API is generally a complementary extension of the AWT rather than
a direct replacement. In fact, every Swing lightweight interface ultimately exists within
an AWT heavyweight component because all of the top-level components in Swing
(JApplet, JDialog,JFrame, and JWindow) extend an AWT top-level container. Prior
to Java 6 Update 10, the use of both lightweight and heavyweight components within
the same window was generally discouraged due to Z-order incompatibilities. However,
later versions of Java have fixed these issues, and both Swing and AWT components
can now be used in one GUI without Z-order issues.
The core rendering functionality used by Swing to draw its lightweight components is
provided by Java 2D, another part of JFC.
9 | P a g e
Java Components
10 | P a g e
JFrame
The components added to the frame are referred to as its contents; these are managed by the content Pane. To add a component to a JFrame, we must use its content Pane instead. JFrame is a Window with border, title and buttons. When JFrame is set visible, an event dispatching thread is started. JFrame objects store several objects including a Container object known as the content pane. To add a component to a JFrame, add it to the content pane.
Creating JFrame Window
Step 1: Construct an object of the JFrame class.
Step 2: Set the size of the JFrame.
Step 3: Set the title of the JFrame to appear in the title bar (title bar will be blank if no title is set).
Step 4: Set the default close operation. When the user clicks the close button, the program stops running.
Step 5: Make the JFrame visible.
11 | P a g e
JLabel
JLabel, descended from JComponent, is used to create text labels.
A JLabel object provides text instructions or information on a GUI — display a single line of read-only text, an image or both text and image.
We use a Swing JLabel when we need a user interface component that displays a message or an image.
12 | P a g e
JButton
A button is a component the user clicks to trigger a specific action.
There are several types of buttons in Java, all are subclasses of AbstractButton.
command buttons: is created with class JButton. It generates ActionEvent.
toggle buttons: have on/off or true/false values.
check boxes: a group of buttons. It generates ItemEvent.
radio buttons: a group of buttons in which only one can be selected. It generates ItemEvent.
13 | P a g e
Example:-
// Import the swing and AWT classes needed
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class SwingExample
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
JFrame f = new JFrame("Swing Example Window");
f.setLayout(new FlowLayout());
f.add(new JLabel("Hello, world!"));
f.add(new JButton("Press me!"));
f.pack();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
14 | P a g e
}
);
}
}
15 | P a g e
References:
http://en.wikipedia.org/wiki/Swing_(Java)
Matthew Robinson, Pavel Vorobiev: Swing, Second Edition, Manning, ISBN 1-930110-88-X
http://www.google.co.in/ #sclient=psy&hl=en&source=hp&q=swings+in+java&pbx=1&oq=swings+in+java&aq=f&aqi=g5&aql=&gs_sm=e&gs_upl=170399l176171l0l176515l21l14l1l0l0l2l1764l7815l4-2.2.1.1.2l9l0&bav=on.2,or.r_gc.r_pw.&fp=cc63231dd3ba60f6&biw=1280&bih=869
http://docs.google.com/viewer? a=v&q=cache:P8TA9pgDM1YJ:waltermilner.com/downloads/JavaSwing/Java%2520Swing.ppt+swings+in+java+ppt&hl=en&gl=in&pid=bl&srcid=ADGEESiEz8NwezOYwyipXPonXBC0nWcz1oAqRU3dKPsqMlEJzV9_ZTtfxoyBy3gyROz859XKJYIoS7867YIKWIztvGsvf1wG68QBiyaV65GYPYkeDFfJ8XujEaoQOFWJF-
vmzmGZe0VH&sig=AHIEtbRatSwTaKEOa7DH8iV2tFaZPyCAbQ
16 | P a g e