Introduction to Java Swing

Download Report

Transcript Introduction to Java Swing

Introduction to Java Swing
“We are the sultans of swing” – Mark Knopfler
What it is

Swing is the primary Java GUI widget toolkit. It is part of Oracle's Java
Foundation Classes (JFC) — an API for providing a graphical user
interface (GUI) for Java programs.

More sophisticated set of GUI components than the earlier Abstract Window
Toolkit (AWT).

Provides a native look and feel that emulates the look and feel of several
platforms, and also supports a pluggable look and feel

Unlike AWT components, Swing components are not implemented by
platform-specific code. Instead they are written entirely in Java and therefore
are platform-independent.
Swing Features

Light Weight - Swing component are independent of native Operating System's
API as Swing API controls are rendered mostly using pure JAVA code instead of
underlying operating system calls.

Rich controls - Swing provides a rich set of advanced controls like Tree,
TabbedPane, slider, colourpicker, table controls

Highly Customizable - Swing controls can be customized in very easy way as
visual appearance is independent of internal representation.

Pluggable look-and-feel- SWING based GUI Application look and feel can be
changed at run time based on available values.
Architecture (MVC)
Swing API architecture follows loosely based MVC architecture in the following manner.

A Model represents component's data.

View represents visual representation of the component's data.

Controller takes the input from the user on the view and reflects the changes in
Component's data.

Swing component have Model as a separate element and View and Controller part
are clubbed in User Interface elements. Due to this, Swing has pluggable lookand-feel architecture.
Swing Controls

UI elements : The core visual elements the user eventually sees and interacts with.
GWT provides a huge list of widely used and common elements varying from basic
to complex which we will cover in this tutorial.

Layouts: They define how UI elements should be organized on the screen and
provide a final look and feel to the GUI (Graphical User Interface). This part will be
covered in Layout chapter.

Behavior: These are events which occur when the user interacts with UI elements.
This part will be covered in Event Handling chapter.
Important Classes

Component Class : The class Component is the abstract base class for the non menu user-interface
controls of AWT. Component represents an object with graphical representation.

Container Class: The class Container is the super class for the containers of AWT. Container object
can contain other AWT components.

JComponent Class: The class JComponent is the base class for all Swing components except top-level
containers. To use a component that inherits from JComponent, you must place the component in a
containment hierarchy whose root is a top-level Swing container.

JLabel Class: The class JLabel can display either text, an image, or both. Label's contents are aligned by
setting the vertical and horizontal alignment in its display area. By default, labels are vertically centered
in their display area. Text-only labels are leading edge aligned, by default; image-only labels are
horizontally centered, by default.

JButton Class: The class JButton is an implementation of a push button. This component has a label and
generates an event when pressed. It can have Image also.
Etc………………
Important Interfaces
SWING Event Listeners  The Event listener represent the interfaces responsible to handle
events. Java provides us various Event listener classes but we will discuss those which are more
frequently used. Every method of an event listener method has a single argument as an object which
is subclass of EventObject class. For example, mouse event listener methods will accept instance of
MouseEvent, where MouseEvent derives from EventObject.

ActionListener Interface : The class which processes the ActionEvent should implement this
interface.The object of that class must be registered with a component.The object can be
registered using the addActionListener() method. When the action event occurs, that object's
actionPerformed method is invoked.

ComponentListener Interface: The class which processes the ComponentEvent should
implement this interface.The object of that class must be registered with a component.The
object can be registered using the addComponentListener() method. Component event are
raised for information only.
Etc……

Look into code samples…..
Sources (Most important part)

http://www.tutorialspoint.com/swing/swing_quick_guide.htm

http://www.tutorialspoint.com/swing/

http://en.wikipedia.org/wiki/Swing_(Java)

http://docs.oracle.com/javase/tutorial/uiswing/