swings in java
TRANSCRIPT
-
8/3/2019 Swings in Java
1/11
SWINGS IN JAVAPROGRAMMING IN JAVA ASSIGNMENT
FACULTY : MS. CHARU WAHI
SALONI NANDA BBA4519/09
SHUBHI SINGH BBA4530/09
SALONI SHARMA BBA4542/09
NIHARIKA CHUGH BBA4547/09
10/25/2010
-
8/3/2019 Swings in Java
2/11
WHAT IS A SWING?
To create a Java program with a graphical user interface (GUI), we use swings in
java .The Swing toolkit includes a rich set of components for building GUIs and
adding interactivity to Java applications. Swing includes all the components you
would expect from a modern toolkit: table controls, list controls, tree controls, buttons,
and labels.
Swing is far from a simple component toolkit, however. It includes rich undo support,
a highly customizable text package, integrated internationalization and accessibility
support. To truly leverage the cross-platform capabilities of the Java platform, Swing
supports numerous look and feels, including the ability to create your own look and
feel. The ability to create a custom look and feel is made easier with Synth, a look
and feel specifically designed to be customized. Swing wouldn't be a component
toolkit without the basic user interface primitives such as drag and drop, event
handling, customizable painting, and window management.
Swing is part of the Java Foundation Classes (JFC). The JFC also include other
features important to a GUI program, such as the ability to add rich graphics
functionality and the ability to create a program that can work in different languages
and by users with different input devices.
FEATURES OF SWINGS
The following list shows some of the features that Swing and the Java Foundation Classes
provide.
Swing GUI Components
The Swing toolkit includes a rich array of components: from basic components, such
as buttons and check boxes, to rich and complex components, such as tables and
text. Even deceptively simple components, such as text fields, offer sophisticated
functionality, such as formatted text input or password field behavior. There are file
browsers and dialogs to suit most needs, and if not, customization is possible. If
none of Swing's provided components are exactly what you need, you can leverage
the basic Swing component functionality to create your own.
-
8/3/2019 Swings in Java
3/11
Java 2D API
To make your application stand out; convey information visually; or add figures,
images, or animation to your GUI, you'll want to use the Java 2DTM API. Because
Swing is built on the 2D package, it's trivial to make use of 2D within Swing
components. Adding images, drop shadows, compositingit's easy with Java 2D.
Pluggable Look-and-Feel Support
Any program that uses Swing components has a choice of look and feel. The JFC
classes shipped by Sun and Apple provide a look and feel that matches that of the
platform. The Synth package allows you to create your own look and feel. The GTK+
look and feel makes hundreds of existing look and feels available to Swing programs.
A program can specify the look and feel of the platform it is running on, or it canspecify to always use the Java look and feel, and without recompiling, it will just work.
Data Transfer
Data transfer, via cut, copy, paste, and drag and drop, is essential to almost any
application. Support for data transfer is built into Swing and works between Swing
components within an application, between Java applications, and between Java
and native applications.
Internationalization
This feature allows developers to build applications that can interact with users
worldwide in their own languages and cultural conventions. Applications can be
created that accept input in languages that use thousands of different characters,
such as Japanese, Chinese, or Korean.
Swing's layout managers make it easy to honor a particular orientation required by
the UI. For example, the UI will appear right to left in a locale where the text flows
right to left. This support is automatic: You need only code the UI once and then it
will work for left to right and right to left, as well as honor the appropriate size of
components that change as you localize the text.
Accessibility API
People with disabilities use special softwareassistive technologiesthat
mediates the user experience for them. Such software needs to obtain a wealth of
information about the running application in order to represent it in alternate media:
-
8/3/2019 Swings in Java
4/11
for a screen reader to read the screen with synthetic speech or render it via a Braille
display, for a screen magnifier to track the caret and keyboard focus, for on-screen
keyboards to present dynamic keyboards of the menu choices and toolbar items and
dialog controls, and for voice control systems to know what the user can control withhis or her voice. The accessibility API enables these assistive technologies to get the
information they need, and to programmatically manipulate the elements that make
up the graphical user interface.
Undo Framework API
Swing's undo framework allows developers to provide support for undo and redo.
Undo support is built in to Swing's text component. For other components, Swing
supports anunlimited
number of actions to undo and redo, and is easily adapted toan application.
THE SWING ARCHITECTURE
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.Swing is a component-based framework. The distinction between
objects and components is a fairly subtle point: concisely, a component is a well-
behaved object with a known/specified characteristic pattern of behaviour. Swing
objects asynchronously fire events, have "bound" properties, and respond to a well-
known set of commands (specific to the component.) Specifically, Swing
components are Java Beans components, compliant with the Java Beans
Component Architecture specifications...
-
8/3/2019 Swings in Java
5/11
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
programmaticaly 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.
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.
Lightweight 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
-
8/3/2019 Swings in Java
6/11
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.
Loosely-Coupled and MVC
The Swing library makes heavy use of the Model/View/Controllersoftware design
pattern[1], which conceptually decouples the data being viewed from the user
interface controls through which it is viewed. Because of this, most Swing
components have associated models (which are specified in terms of Java
interfaces), and the programmer can use various default implementations or provide
their own. The framework provides default implementations of model interfaces for
all of its concrete components. The typical use of the Swing framework does not
require the creation of custom models, as the framework provides a set of default
implementations that are transparently, by default, associated with the corresponding
JComponent child class in the Swing library. In general, only complex components,
such as tables, trees and sometimes lists, may require the custom model
implementations around the application-specific data structures. To get a goodsense of the potential that the Swing architecture makes possible, consider the
hypothetical situation where custom models for tables and lists are wrappers over
DAO and/orEJB services.
Typically, Swing component model objects are responsible for providing a concise
interface defining events fired, and accessible properties for the (conceptual) data
model for use by the associated JComponent. Given that the overall MVC pattern is
a loosely-coupled collaborative object relationship pattern, the model provides the
programmatic means for attaching event listeners to the data model object. Typically,
these events are model centric (ex: a "row inserted" event in a table model) and are
mapped by the JComponent specialization into a meaningful event for the GUI
component.The view component of a Swing JComponent is the object used to
graphically "represent" the conceptual GUI control. A distinction of Swing, as a GUI
framework, is in its reliance on programmatically-rendered GUI controls (as opposed
to the use of the native host OS's GUI controls). Prior to Java 6 Update 10, this
http://en.wikipedia.org/wiki/Model-view-controllerhttp://en.wikipedia.org/wiki/Design_pattern_(computer_science)http://en.wikipedia.org/wiki/Design_pattern_(computer_science)http://en.wikipedia.org/wiki/Swing_(Java)#cite_note-0http://en.wikipedia.org/wiki/Swing_(Java)#cite_note-0http://en.wikipedia.org/wiki/Interface_(computer_science)http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.htmlhttp://en.wikipedia.org/wiki/Data_Access_Objectshttp://en.wikipedia.org/wiki/Ejbhttp://en.wikipedia.org/wiki/Java_6#Java_SE_6_Update_10http://en.wikipedia.org/wiki/Java_6#Java_SE_6_Update_10http://en.wikipedia.org/wiki/Ejbhttp://en.wikipedia.org/wiki/Data_Access_Objectshttp://java.sun.com/javase/6/docs/api/javax/swing/JComponent.htmlhttp://en.wikipedia.org/wiki/Interface_(computer_science)http://en.wikipedia.org/wiki/Swing_(Java)#cite_note-0http://en.wikipedia.org/wiki/Design_pattern_(computer_science)http://en.wikipedia.org/wiki/Design_pattern_(computer_science)http://en.wikipedia.org/wiki/Model-view-controller -
8/3/2019 Swings in Java
7/11
distinction was a source of complications when mixing AWT controls, which use
native controls, with Swing controls in a GUI (see Mixing AWT and Swing
components).
Finally, in terms of visual composition and management, Swing favors relative
layouts (which specify the positional relationships between components) as opposed
to absolute layouts (which specify the exact location and size of components). This
bias towards "fluid"' visual ordering is due to its origins in the applet operating
environment that framed the design and development of the original Java GUI toolkit.
(Conceptually, this view of the layout management is quite similar to that which
informs the rendering of HTML content in browsers, and addresses the same set of
concerns that motivated the former.)
Relationship to AWT
http://en.wikipedia.org/wiki/Abstract_Window_Toolkit#Mixing_AWT_and_Swing_componentshttp://en.wikipedia.org/wiki/Abstract_Window_Toolkit#Mixing_AWT_and_Swing_componentshttp://en.wikipedia.org/wiki/Layout_managerhttp://en.wikipedia.org/wiki/Layout_managerhttp://en.wikipedia.org/wiki/Java_applethttp://en.wikipedia.org/wiki/File:AWTSwingClassHierarchy.pnghttp://en.wikipedia.org/wiki/Java_applethttp://en.wikipedia.org/wiki/Layout_managerhttp://en.wikipedia.org/wiki/Layout_managerhttp://en.wikipedia.org/wiki/Abstract_Window_Toolkit#Mixing_AWT_and_Swing_componentshttp://en.wikipedia.org/wiki/Abstract_Window_Toolkit#Mixing_AWT_and_Swing_components -
8/3/2019 Swings in Java
8/11
Relationship to SWT
The Standard Widget Toolkit (SWT) is a competing toolkit originally developed by
IBM and now maintained by the Eclipse community. SWT's implementation has more
in common with the heavyweight components of AWT. This confers benefits such as
more accurate fidelity with the underlying native windowing toolkit, at the cost of an
increased exposure to the native platform in the programming model.The advent of
SWT has given rise to a great deal of division among Java desktop developers, with
many strongly favoring eitherSWT or Swing. Sun's development on Swing continues
to focus on platform look and feel (PLAF) fidelity with each platform's windowing
toolkit in the approaching Java SE 7 release (as of December 2006).
There has been significant debate and speculation about the performance of SWT
versus Swing; some hinted that SWT's heavy dependence on JNI would make it
slower when the GUI component and Java need to communicate data, but faster at
rendering when the data model has been loaded into the GUI, but this has not been
confirmed either way[2]. A fairly thorough set of benchmarks concluded that neither
Swing nor SWT clearly outperformed the other in the general case[3].
SWT serves the Windows platform very well but is considered by some[who?] to be
less effective as a technology for cross-platform development. By using the high-
level features of each native windowing toolkit, SWT returns to the issues seen in the
mid 90's (with toolkits like zApp, Zinc, XVT and IBM/Smalltalk) where toolkits
attempted to mask differences in focus behaviour, event triggering and graphical
layout. Failure to match behavior on each platform can cause subtle but difficult-to-
resolve bugs that impact user interaction and the appearance of the GUI.Swing
provides a richer set of components than AWT. They are 100% Java-based.
AWT on the other hand was developed with the mind set that if a component or
capability of a component wasn't available on one platform, it won't be available on
any platform. Something quickly portable from platform x, to y, to z. Due to the peer-
based nature of AWT, what might work on one implementation might not work on
another, as the peer-integration might not be as robust. Many of the original AWT
problems were traceable to differences in peer implementations.
http://en.wikipedia.org/wiki/Standard_Widget_Toolkithttp://en.wikipedia.org/wiki/Java_Platform,_Standard_Editionhttp://en.wikipedia.org/wiki/Java_Native_Interfacehttp://en.wikipedia.org/wiki/Swing_(Java)#cite_note-1http://en.wikipedia.org/wiki/Swing_(Java)#cite_note-1http://en.wikipedia.org/wiki/Swing_(Java)#cite_note-2http://en.wikipedia.org/wiki/Swing_(Java)#cite_note-2http://en.wikipedia.org/wiki/Wikipedia:Avoid_weasel_wordshttp://en.wikipedia.org/wiki/Wikipedia:Avoid_weasel_wordshttp://en.wikipedia.org/wiki/Wikipedia:Avoid_weasel_wordshttp://en.wikipedia.org/wiki/Wikipedia:Avoid_weasel_wordshttp://en.wikipedia.org/wiki/Swing_(Java)#cite_note-2http://en.wikipedia.org/wiki/Swing_(Java)#cite_note-1http://en.wikipedia.org/wiki/Java_Native_Interfacehttp://en.wikipedia.org/wiki/Java_Platform,_Standard_Editionhttp://en.wikipedia.org/wiki/Standard_Widget_Toolkit -
8/3/2019 Swings in Java
9/11
There are a few other advantages to Swing over AWT:
Swing provides both additional components and added functionality to AWT-
replacement components
Swing components can change their appearance based on the current "look
and feel" library that's being used. You can use the same look and feel as the
platform you're on, or use a different look and feel
Swing components follow the Model-View-Controller paradigm (MVC), and
thus can provide a much more flexible UI.
Swing provides "extras" for components, such as:
o Icons on many componentso Decorative borders for componentso Tooltips for components
Swing components are lightweight (less resource intensive than AWT)
Swing provides built-in double buffering
Swing provides paint debugging support for when you build your own
components
Swing also has a few disadvantages:
It requires Java 2 or a separate JAR file
If you're not very careful when programming, it can be slower than AWT (all
components are drawn)
Swing components that look like native components might not act exactly likenative components
-
8/3/2019 Swings in Java
10/11
Java Swing class hierarchy
The class JComponent, descended directly from Container, is the root class for most
of Swings user interface components.
Java Swing Examples
Below is a java swing code for the traditional Hello World program.
Basically, the idea behind this Hello World program is to learn how to
create a java program, compile and run it. To create your java source
code you can use any editor or you can use an IDE like Eclipse.
import javax.swing.JFrame;
import javax.swing.JLabel;
//import statements
//Check if window closes automatically. Otherwise add suitable code
public class HelloWorldFrame extends JFrame {
-
8/3/2019 Swings in Java
11/11
public static void main(String args[]) {
new HelloWorldFrame();
}
HelloWorldFrame() {
JLabel jlbHelloWorld = new JLabel("Hello World");
add(jlbHelloWorld);
this.setSize(100, 100);
// pack();
setVisible(true);
}
}
Output