SlideShare a Scribd company logo
WWW.ThesisScientist.com
Advanced Swing
Swings –
Swing is a widget toolkit for Java. It is part of Sun Microsystems' 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.
Swing is a platform-independent, Model-View-Controller GUI framework for
Java. It follows a single-threaded programming model, and possesses the
following traits:
Features:
Platform independence
Swing is platform independent both in terms of its expression (Java) and its
implementation (non-native universal rendering of widgets).
Extensibility
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.
Component-oriented
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 behavior. 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.
WWW.ThesisScientist.com
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 this property (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 on the graphic device in response to a call to
component.paint(), which is defined in (AWT) Container. But unlike AWT
WWW.ThesisScientist.com
components, which delegated the painting to their OS-native "heavyweight"
widget, Swing components are responsible for their own rendering.
This transposition and decoupling is not merely visual, and extends to Swing's
management and application of its own OS-independent semantics for events
fired within its component containment hierarchies. Generally speaking, the
Swing Architecture delegates the task of mapping the various flavors of OS GUI
semantics onto a simple, but generalized, pattern to the AWT container.
Building on that generalized platform, it establishes its own rich and complex
GUI semantics in the form of the JComponent model. A review of the source of
Container java and JComponent java classes is recommended for further
insights into the nature of the interface between Swing's lightweight
components and AWT's heavyweight widgets.
Loosely-Coupled/MVC
The Swing library makes heavy use of
the Model/View/Controller software design pattern, 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.
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.
For example, the JTable has a model called TableModel that describes an
interface for how a table would access tabular data. A default implementation
of this operates on a two-dimensional array.
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
WWW.ThesisScientist.com
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 distinction was a source of complications when mixing AWT controls,
which use native controls, with Swing controls in a GUI.
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.)
Look and feel
Swing allows one to specialize the look and feel of widgets, by modifying the
default (via runtime parameters), deriving from an existing one, by creating one
from scratch, or, beginning withJ2SE 5.0, by using the skinnable synth Look
and Feel, which is configured with an XML property file. The look and feel can
be changed at runtime, and early demonstrations of Swing frequently provided
a way to do this.
Lists –
If you want to present a set of choices to a user, and a radio button or
checkbox set consumes too much space, you can use a combo box or a list.
The JList component has many more features, and its design is similar to that
of the tree and table components. The list control is somewhat awkward to use
for common cases because you need to manipulate some of the machinery that
makes the general cases possible.
The JList Component
The JList component is similar to a set of checkboxes or radio buttons, except
that the items are placed inside a single box and are selected by clicking on the
items themselves, not on buttons. If you permit multiple selections for a list
box, the user can select any combination of the items in the box.
WWW.ThesisScientist.com
To construct this list component, you first start out with an array of strings,
then pass the array to the JList constructor :
String[ ] words= { "quick", "brown", "hungry", "wild", ... };
JList wordList= newJList(words);
List boxes do not scroll automatically. To make a list box scroll, you must
insert it into a scroll pane:
JScrollPane scroll Pane=new JScrollPane(wordList);
You then add the scroll pane, not the list, into the surrounding panel. By
default, the list component displays eight items; use the setVisibleRowCount
method to change that value:
wordList.setVisibleRowCount(4); // displays 4 items
You can set the layout orientation to one of three values:
 JList.VERTICAL (the default): arrange all items vertically.
 List.VERTICAL_WRAP: start new columns if there are more items than
the visible row count.
 List.HORIZONTAL_WRAP: start new columns if there are more items
than the visible row count, but fill them horizontally.
You can also restrict the user to a more limited selection mode with the
setSelectionMode method:
wordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
/ / select one item at a time
wordList.setSelectionModel(ListSelectionModel.SINGLE_INTERVAL_SELECTlON);
/ / select one item or one range of items
The basic user interface components send out action events when the user
activates them. So u need to add a list selection listener to the list component,
and implement the method
public void valueChanged(ListSelectionEvent evt)
in the listener. When the user selects items, a flurry of list selection events is
generated.
Once you are notified that an event has happened, you will want to find out
what items are currently selected. The getSelectedValues method returns an
WWW.ThesisScientist.com
array of objects containing all selected items. Cast each array element to a
string.
Object[ ] values =list.getSelectedValues();
for(Object value : values)
do something with (String) value;
List Models
The most common method for using a list component.
 Specify a fixed set of strings for display in the list.
 Place the list inside a scroll pane, and
 Trap the list selection events.
The JList class is responsible for the visual appearance of the data. It actually
knows very little about how the data are stored-all it knows is that it can
retrieve the data through some object that implements the ListModel interface:
public interface ListModel
{
int getSize () ;
Object getElementAt( int i) ;
void addListDataListener(ListDataListener l) ;
void removeListDataListener(ListDataListener l):
}
Through this interface, the JList can get a count of elements and retrieve each
one of the elements.
WWW.ThesisScientist.com
Also, the JList object can add itself as a list data listener. That way, if the
collection of elements changes, JList gets notified so that it can repaint the list.
Adding and removing listeners, has been done in the AbstractListModel class,
which we extend. We only need to supply the getSize and getElementAt
methods:
class WordListModel extends AbstractListModel
{
public WordListModel(int n)
{
length = n;
}
public int getSize()
{
return (int) Math.pow(26,length);
}
public Object getElementAt(int n)
{
II compute nth string
}
(draw diagram on page no 349)
Inserting and Removing Values –
You cannot directly edit the collection of list values. Instead, you must access
the model and then add or remove elements. Suppose you want to add more
values to a list. You can obtain a reference to the model:
ListModel model=list.getModel() ;
But the ListModel interface also has no methods to insert or remove elements
because a list model is that it need not store the elements.
Instead, you should construct a DefaultListModel object, fill it with the initial
values, and associate it with the list.
DefaultListModel model = new DefaultListModel() ;
model.addElement("quick");
model.addElement("brown");
……………
JList list = newJList(model) ;
WWW.ThesisScientist.com
Now you can add or remove values from the model object. The model object
then notifies the list of the changes, and the list repaints itself.
model.removeElement( "quick");
model.addElement("slow") ;
Rendering Values –
You can easily represent your list values with any drawing whatsoever.
Although the JList class can display strings and icons automatically, you need
to install a list cell renderer into the JList object for all custom drawing. A list
cell renderer is any class that implements the following interface:
interface ListCellRenderer
{
Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected, boolean cell HasFocus) ;
}
This method is called for each cell. It returns a component that paints the cell
contents. The component is placed at the appropriate location whenever a cell
needs to be rendered.
If the rendered image just contains text, an icon, and possibly a change of
color, then you can get by with configuring a JLabel.For eg, to show the font
name in its own font, we can use the following renderer:
class FontCellRenderer extends JLabel implements ListCellRenderer
{
public Component getListCellRendererComponent(JList list, Object value, int
index, boolean isSelected, Boolean cellHasFocus)
{
JLabel label = new JLabel() ;
Font font = (Font) value ;
setText(font.getFamily()) ;
setFont(font) ;
setOpaque(true) ;
setBackground(isSelected?list.getSelectionBackground():list.getBackground());
setForeground(isSelected?list.getSelectionForeground():list.getForeground());
return this ;
}
}
TREES –
WWW.ThesisScientist.com
Many tree structures arise in everyday life,such as the hierarchy of countries ,states & cities.The Swing
library has a JTree class for this purpose.The JTree class takes care of laying out the tree and processing
user requests for expanding and collapsing nodes. A tree is composed of nodes.Every node is either a leaf
or it has child nodes.Everynode,with the exception of the root node, has exactly one parent.A tree has
exactly one root node. Sometimes you have a collection of trees, each of which has its own root node.Such
a collection is called a forest.
(draw diagram on page no 358 Fig 6.7 )
Simple Trees -
The JTree component follows the model view-controller pattern. You provide a model of the hierarchical
data, and the component displays it for you. To construct a JTree, you supply the tree model in the
constructor TreeModel model=. . . JTreee tree = new JTree(model) ;
To construct a default tree model, you must supply a root node.
TreeNode root = . . . DefaultTreeModel model= newDefaultTreeModel(root) ;
TreeNode is another interface.
you can simply pass the root node to the JTree constructor.
Then the tree automatically constructs a default tree model :
JTree tree =new JTree(root) ;
Editing Trees and Tree Paths -
The JTree class has a surprising way of identifying nodes in a tree. It doesnot deal with tree nodes, but
with paths of objects,called tree paths. A tree path starts at the root & consists of a sequence of child
nodes.
(draw diagram on page no 366 Fig 6-18)
The JTree class knows nothing about the TreeNode interface.That interace is never used by the TreeModel
interface.it is only used by the DefaultTreeModel implementation.You can have other tree models in which
the nodes do not implement the TreeNode interface at all.If you use a tree model that manages other types
of objects, then those objects may not have getParent & getChild methods. The TreePath class manages a
sequence of Object references.
A number of JTree methods return Treepath objects.When you have a tree path,you usually just need to
Know the terminal node,which you get with the getLastPathComponent method.For eg, to find out the
currently selected node in a tree, you use the getSelectionPath method of the JTree
class.You get a TreePath object back, from which you can retrieve the actual node.
TreePath selectionPath= tree.getSelectionPath() ;
DefaultMutableTreeNode selectedNode =
(DefaultMutableTreeNode)
selectionPath.getLastPathComponent() ;
If you use the insertNodeInto method of the DefaultTreeModel class, the model class takes care of that. For
eg, the following call appends a new node as the last child of the selected node & notifies the tree view.
WWW.ThesisScientist.com
model.insertNodelnto(newNode, selectedNode ,
selectedNode.getchildCount()) ;
The analogous call removeNodeFromParent removes a node and notifies the view:
model.removeNodeFromParent (selectedNode) ;
Node Enumeration -
Sometimes you need to find a node in a tree by starting at the root & visiting all children until you have
found a match.The DefaultMutableTreeNode class has several methods for iterating through nodes. The
breadthFirstEnumeration & depthFirstEnumeration methods return enumeration objects whose
nextElement method visits all children of the current node,using either a breadth-first or depth-first
traversal. Breadth-first enumeration is the easiest to visualize.The tree is traversed in layers.The root is
visited first,followed by all of its children, then followed by the grandchildren, and so on.To visualize depth-
first enumeration,imagine a rat trapped in a tree-shaped maze. It rushes along the first path until it comes to
a leaf.Then, it backtracks and turns around to the next path, and so on. The postOrderTraversal method is
a synonym for depthFirstTraversal because the search process visits the childen before visiting the
parents.
Here is the typical usage pattern:
Enumeration breadthFirst = node.breadthFirstEnumeration() ;
while (breadthFirst.hasMoreElements())
do something with breadthFirst. nextElement();
Finally,a method, pathFromAncestorEnumeration, finds a path from an ancestor to a given node and then
enumerates the nodes along that path.
Rendering Nodes-
you will often need to change the way in which a tree component draws the nodes.The most common
change is to choose different icons for nodes and leaves.Other changes might involve changing the font of
the node labels or drawing images at the nodes. All these changes are made possible by installing a new
tree cell renderer into the tree.By default, the JTree class uses DefaultTreeCellRenderer objects to draw
each node.The DefaultTreeCellRenderer class extends the JLabelclass. The label contains the node icon
and the node label.
You can customize the display in three ways :
1. You can change the icons, font, and background color used
by a DefaultTreeCellRenderer.These settings are used for
all nodes in the tree.
2. You can install a renderer that extends the
DefaultTreeCellRenderer class and vary the icons, fonts,
and background color for each node.
3. You can install a renderer that implements the
TreeCellRenderer interface, to draw a custom
image for each node.
To change the appearance of individual nodes,you install a tree cell renderer.The TreeCellRenderer
interface has a single method:
Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
boolean expanded, boolean leaf, int row, boolean hasFocus)
WWW.ThesisScientist.com
Custom Tree Models -
The TreeModel interface has only a handful of methods.The first group of methods enables the JTree to
find the tree nodes by first getting the root, then the children.The JTree class calls these methods only
when the user actually expands a node.
Object getRoot ()
int getChildCount(Object parent)
Object getChild(Object parent, int index)
TheTreeModel is responsible for telling the JTree how they are connected.The next method of the
TreeModel interface is the reverse of getChild:
int getlndexOfChild(Object parent,Object child)
The tree model tells the JTree which nodes should be displayed as leaves:
boolean isLeaf(Object node)
If your code changes the tree model, then the tree needs to be notified so that it can redraw itself. The tree
adds itself as a TreeModelListener to the model.Thus, the model must support the usual listener
management methods:
void addTreeModelListener(TreeModelListener l)
void removeTreeModelListener(TreeModelListener l)
When the model modifies the tree contents, it calls one of the four methods of the TreeModelListener
interface:
void treeNodesChanged(TreeModelEvent e)
void treeNodeslnserted(TreeModelEvent e)
void treeNodesRemoved (TreeModelEvent e )
void treeStructureChanged(TreeModelEvent e)
The TreeModelEvent object describes the location of the change.
Finally if the user edits a tree node, your model is called with the change.
Void valueForPathChanged(TreePath path ,Object newValue)
If you don’t allow editing, this method is never called.
Progress Indicators -
A JProgressBar is a Swing component that indicates progress. A ProgressMonitor is a dialog box that
contains a progress bar. A ProqressMonitorInputStream displays a progress monitor
dialog box while the stream is read.
Progress Bars
A progressbar is a simple component-just a rectangle that is partially filled with color to indicate the
progress of an operation. By default, progress is indicated by a string “n% “.
You construct a progress bar much as you construct a slider, by supplying the minimum and maximum
value and an optional orientation:
progressBar = newJProgressBar(0, 1000);
progressBar= newJProgressBar(SwingConstants. VERTICAL, 0, 1000);
WWW.ThesisScientist.com
You can also set the minimum and maximum with the setMinimum and setMaximum methods.
Unlike a slider,the progress bar cannot be adjusted by the user. Your program needs to call setValueto
update it.
If you call
progressBar. setStringPainted(true);
the progress bar computes the completion percentage and displays a string ”n% “ .If you want to show a
different string, you can supply it with the setString method:
if (progressBar.getValue() > 900)
progressBar.setString(" AlmostDone ");
Progress Monitor
A ProgressMonitor is a complete dialog box that contains a progress bar.The dialog box contains a Cancel
button.If you click it,the monitor dialog box is closed.In addition,your program can query whether the user
has canceled the dialog box & terminate the monitored action.
You construct a progress monitor by supplying the following:
 The parent component over which the dialog box should pop up.
 An object that is displayed on the dialog box.
 An optional note to display below the object.
 The minimum and maximum values. .
As you update the progress value, you should also call the isCanceled method to see if the program user
has clicked the
Cancel button.
When the monitored activity has concluded, call the close method to dismiss the dialog box.You can reuse
the same dialog box by calling start again.
There are two conditions for termination. The activity might have completed, or the user might have
canceled it. In each of these cases, we close down :
 The timer that monitored the activity.
 The progress dialog box.
 The activity itself (by interrupting the thread).
Component Organizers –
These include the split pane, a mechanism for splitting an area into multiple parts whose boundaries canbe
adjusted,the tabbed pane,which uses tab dividers to allow a user to flip through multiple panels,& the
desktop pane.,which can be used to implement applications that display multiple internal frames.

More Related Content

What's hot

JAVA GUI
JAVA GUIJAVA GUI
JAVA GUIIIITA
 
java swing programming
java swing programming java swing programming
java swing programming
Ankit Desai
 
Java Swing
Java SwingJava Swing
Java Swing
Shraddha
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
Adil Mehmoood
 
Java Swing
Java SwingJava Swing
Java Swing
Arkadeep Dey
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
yht4ever
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
babak danyal
 
Gui
GuiGui
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A Review
Fernando Torres
 
Windows Programming with Swing
Windows Programming with SwingWindows Programming with Swing
Windows Programming with Swingbackdoor
 
L0018 - SWT - The Standard Widget Toolkit
L0018 - SWT - The Standard Widget ToolkitL0018 - SWT - The Standard Widget Toolkit
L0018 - SWT - The Standard Widget Toolkit
Tonny Madsen
 
GUI programming
GUI programmingGUI programming
GUI programming
Vineeta Garg
 
Swing
SwingSwing
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
Matlab GUI
Matlab GUIMatlab GUI
Complete java swing
Complete java swingComplete java swing
Complete java swing
jehan1987
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
Ankit Dubey
 
Java swing
Java swingJava swing
Java swing
Apurbo Datta
 

What's hot (20)

JAVA GUI
JAVA GUIJAVA GUI
JAVA GUI
 
java swing programming
java swing programming java swing programming
java swing programming
 
Java Swing
Java SwingJava Swing
Java Swing
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
Java Swing
Java SwingJava Swing
Java Swing
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
 
Gui
GuiGui
Gui
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A Review
 
Windows Programming with Swing
Windows Programming with SwingWindows Programming with Swing
Windows Programming with Swing
 
L0018 - SWT - The Standard Widget Toolkit
L0018 - SWT - The Standard Widget ToolkitL0018 - SWT - The Standard Widget Toolkit
L0018 - SWT - The Standard Widget Toolkit
 
GUI programming
GUI programmingGUI programming
GUI programming
 
Introdu.awt
Introdu.awtIntrodu.awt
Introdu.awt
 
Swing
SwingSwing
Swing
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
 
Matlab GUI
Matlab GUIMatlab GUI
Matlab GUI
 
Complete java swing
Complete java swingComplete java swing
Complete java swing
 
04b swing tutorial
04b swing tutorial04b swing tutorial
04b swing tutorial
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
 
Java swing
Java swingJava swing
Java swing
 

Similar to Advanced swing

Advanced java lab swing mvc awt
Advanced java lab swing mvc awtAdvanced java lab swing mvc awt
Advanced java lab swing mvc awt
vishal choudhary
 
Swing !!! y shikhar!!
Swing !!! y shikhar!!Swing !!! y shikhar!!
Swing !!! y shikhar!!
shikhar199
 
GUI design using JAVAFX.ppt
GUI design using JAVAFX.pptGUI design using JAVAFX.ppt
GUI design using JAVAFX.ppt
TabassumMaktum
 
Chapter 1-Note.docx
Chapter 1-Note.docxChapter 1-Note.docx
Chapter 1-Note.docx
TigistTilahun1
 
Java swing 1
Java swing 1Java swing 1
Java swing 1
Mukesh Tekwani
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
SURBHI SAROHA
 
Java lab lecture 2
Java  lab  lecture 2Java  lab  lecture 2
Java lab lecture 2
vishal choudhary
 
Mobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdfMobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdf
AbdullahMunir32
 
What is swing
What is swingWhat is swing
What is swing
Sher Singh Bardhan
 
ITE 1122_ AWT and SWING.pptx
ITE 1122_ AWT  and SWING.pptxITE 1122_ AWT  and SWING.pptx
ITE 1122_ AWT and SWING.pptx
udithaisur
 
Java swing
Java swingJava swing
Java swing
ssuser3a47cb
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swings
MohanYedatkar
 
Swing components
Swing components Swing components
Swing components
Sameenafathima4
 
Computer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxComputer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptx
jonathancapitulo2
 
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
AbuSufyan82
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
JONDHLEPOLY
 
Swing is not dead
Swing is not deadSwing is not dead
Swing is not dead
Piotr Dziewonski
 
Java awt tutorial javatpoint
Java awt tutorial   javatpointJava awt tutorial   javatpoint
Java awt tutorial javatpoint
Ricardo Garcia
 

Similar to Advanced swing (20)

Advanced java lab swing mvc awt
Advanced java lab swing mvc awtAdvanced java lab swing mvc awt
Advanced java lab swing mvc awt
 
Swing !!! y shikhar!!
Swing !!! y shikhar!!Swing !!! y shikhar!!
Swing !!! y shikhar!!
 
GUI design using JAVAFX.ppt
GUI design using JAVAFX.pptGUI design using JAVAFX.ppt
GUI design using JAVAFX.ppt
 
Chapter 1-Note.docx
Chapter 1-Note.docxChapter 1-Note.docx
Chapter 1-Note.docx
 
Java swing 1
Java swing 1Java swing 1
Java swing 1
 
Report swings
Report swingsReport swings
Report swings
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
Java lab lecture 2
Java  lab  lecture 2Java  lab  lecture 2
Java lab lecture 2
 
Mobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdfMobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdf
 
What is swing
What is swingWhat is swing
What is swing
 
ITE 1122_ AWT and SWING.pptx
ITE 1122_ AWT  and SWING.pptxITE 1122_ AWT  and SWING.pptx
ITE 1122_ AWT and SWING.pptx
 
Java swing
Java swingJava swing
Java swing
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swings
 
Swt vs swing
Swt vs swingSwt vs swing
Swt vs swing
 
Swing components
Swing components Swing components
Swing components
 
Computer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxComputer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptx
 
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 
Swing is not dead
Swing is not deadSwing is not dead
Swing is not dead
 
Java awt tutorial javatpoint
Java awt tutorial   javatpointJava awt tutorial   javatpoint
Java awt tutorial javatpoint
 

More from Thesis Scientist Private Limited

HTML guide for beginners
HTML guide for beginnersHTML guide for beginners
HTML guide for beginners
Thesis Scientist Private Limited
 
Ransomware attacks 2017
Ransomware attacks 2017Ransomware attacks 2017
Ransomware attacks 2017
Thesis Scientist Private Limited
 
How to write a Great Research Paper?
How to write a Great Research Paper?How to write a Great Research Paper?
How to write a Great Research Paper?
Thesis Scientist Private Limited
 
Research Process design
Research Process designResearch Process design
Research Process design
Thesis Scientist Private Limited
 
How to write a good Dissertation/ Thesis
How to write a good Dissertation/ ThesisHow to write a good Dissertation/ Thesis
How to write a good Dissertation/ Thesis
Thesis Scientist Private Limited
 
How to write a Research Paper
How to write a Research PaperHow to write a Research Paper
How to write a Research Paper
Thesis Scientist Private Limited
 
Internet security tips for Businesses
Internet security tips for BusinessesInternet security tips for Businesses
Internet security tips for Businesses
Thesis Scientist Private Limited
 
How to deal with a Compulsive liar
How to deal with a Compulsive liarHow to deal with a Compulsive liar
How to deal with a Compulsive liar
Thesis Scientist Private Limited
 
Driverless car Google
Driverless car GoogleDriverless car Google
Driverless car Google
Thesis Scientist Private Limited
 
Podcast tips beginners
Podcast tips beginnersPodcast tips beginners
Podcast tips beginners
Thesis Scientist Private Limited
 
Vastu for Career Success
Vastu for Career SuccessVastu for Career Success
Vastu for Career Success
Thesis Scientist Private Limited
 
Reliance jio broadband
Reliance jio broadbandReliance jio broadband
Reliance jio broadband
Thesis Scientist Private Limited
 
Job Satisfaction definition
Job Satisfaction definitionJob Satisfaction definition
Job Satisfaction definition
Thesis Scientist Private Limited
 
Mistakes in Advertising
Mistakes in AdvertisingMistakes in Advertising
Mistakes in Advertising
Thesis Scientist Private Limited
 
Contributor in a sentence
Contributor in a sentenceContributor in a sentence
Contributor in a sentence
Thesis Scientist Private Limited
 
Different Routing protocols
Different Routing protocolsDifferent Routing protocols
Different Routing protocols
Thesis Scientist Private Limited
 
Ad hoc network routing protocols
Ad hoc network routing protocolsAd hoc network routing protocols
Ad hoc network routing protocols
Thesis Scientist Private Limited
 
IPTV Thesis
IPTV ThesisIPTV Thesis
Latest Thesis Topics for Fog computing
Latest Thesis Topics for Fog computingLatest Thesis Topics for Fog computing
Latest Thesis Topics for Fog computing
Thesis Scientist Private Limited
 
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Thesis Scientist Private Limited
 

More from Thesis Scientist Private Limited (20)

HTML guide for beginners
HTML guide for beginnersHTML guide for beginners
HTML guide for beginners
 
Ransomware attacks 2017
Ransomware attacks 2017Ransomware attacks 2017
Ransomware attacks 2017
 
How to write a Great Research Paper?
How to write a Great Research Paper?How to write a Great Research Paper?
How to write a Great Research Paper?
 
Research Process design
Research Process designResearch Process design
Research Process design
 
How to write a good Dissertation/ Thesis
How to write a good Dissertation/ ThesisHow to write a good Dissertation/ Thesis
How to write a good Dissertation/ Thesis
 
How to write a Research Paper
How to write a Research PaperHow to write a Research Paper
How to write a Research Paper
 
Internet security tips for Businesses
Internet security tips for BusinessesInternet security tips for Businesses
Internet security tips for Businesses
 
How to deal with a Compulsive liar
How to deal with a Compulsive liarHow to deal with a Compulsive liar
How to deal with a Compulsive liar
 
Driverless car Google
Driverless car GoogleDriverless car Google
Driverless car Google
 
Podcast tips beginners
Podcast tips beginnersPodcast tips beginners
Podcast tips beginners
 
Vastu for Career Success
Vastu for Career SuccessVastu for Career Success
Vastu for Career Success
 
Reliance jio broadband
Reliance jio broadbandReliance jio broadband
Reliance jio broadband
 
Job Satisfaction definition
Job Satisfaction definitionJob Satisfaction definition
Job Satisfaction definition
 
Mistakes in Advertising
Mistakes in AdvertisingMistakes in Advertising
Mistakes in Advertising
 
Contributor in a sentence
Contributor in a sentenceContributor in a sentence
Contributor in a sentence
 
Different Routing protocols
Different Routing protocolsDifferent Routing protocols
Different Routing protocols
 
Ad hoc network routing protocols
Ad hoc network routing protocolsAd hoc network routing protocols
Ad hoc network routing protocols
 
IPTV Thesis
IPTV ThesisIPTV Thesis
IPTV Thesis
 
Latest Thesis Topics for Fog computing
Latest Thesis Topics for Fog computingLatest Thesis Topics for Fog computing
Latest Thesis Topics for Fog computing
 
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
 

Recently uploaded

Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
Kamal Acharya
 
AIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdfAIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdf
RicletoEspinosa1
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Self-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptxSelf-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptx
iemerc2024
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
Mukeshwaran Balu
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
Kamal Acharya
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 

Recently uploaded (20)

Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
 
AIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdfAIR POLLUTION lecture EnE203 updated.pdf
AIR POLLUTION lecture EnE203 updated.pdf
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Self-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptxSelf-Control of Emotions by Slidesgo.pptx
Self-Control of Emotions by Slidesgo.pptx
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 

Advanced swing

  • 1. WWW.ThesisScientist.com Advanced Swing Swings – Swing is a widget toolkit for Java. It is part of Sun Microsystems' 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. Swing is a platform-independent, Model-View-Controller GUI framework for Java. It follows a single-threaded programming model, and possesses the following traits: Features: Platform independence Swing is platform independent both in terms of its expression (Java) and its implementation (non-native universal rendering of widgets). Extensibility 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. Component-oriented 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 behavior. 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.
  • 2. WWW.ThesisScientist.com 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 this property (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 on the graphic device in response to a call to component.paint(), which is defined in (AWT) Container. But unlike AWT
  • 3. WWW.ThesisScientist.com components, which delegated the painting to their OS-native "heavyweight" widget, Swing components are responsible for their own rendering. This transposition and decoupling is not merely visual, and extends to Swing's management and application of its own OS-independent semantics for events fired within its component containment hierarchies. Generally speaking, the Swing Architecture delegates the task of mapping the various flavors of OS GUI semantics onto a simple, but generalized, pattern to the AWT container. Building on that generalized platform, it establishes its own rich and complex GUI semantics in the form of the JComponent model. A review of the source of Container java and JComponent java classes is recommended for further insights into the nature of the interface between Swing's lightweight components and AWT's heavyweight widgets. Loosely-Coupled/MVC The Swing library makes heavy use of the Model/View/Controller software design pattern, 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. 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. For example, the JTable has a model called TableModel that describes an interface for how a table would access tabular data. A default implementation of this operates on a two-dimensional array. 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
  • 4. WWW.ThesisScientist.com 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 distinction was a source of complications when mixing AWT controls, which use native controls, with Swing controls in a GUI. 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.) Look and feel Swing allows one to specialize the look and feel of widgets, by modifying the default (via runtime parameters), deriving from an existing one, by creating one from scratch, or, beginning withJ2SE 5.0, by using the skinnable synth Look and Feel, which is configured with an XML property file. The look and feel can be changed at runtime, and early demonstrations of Swing frequently provided a way to do this. Lists – If you want to present a set of choices to a user, and a radio button or checkbox set consumes too much space, you can use a combo box or a list. The JList component has many more features, and its design is similar to that of the tree and table components. The list control is somewhat awkward to use for common cases because you need to manipulate some of the machinery that makes the general cases possible. The JList Component The JList component is similar to a set of checkboxes or radio buttons, except that the items are placed inside a single box and are selected by clicking on the items themselves, not on buttons. If you permit multiple selections for a list box, the user can select any combination of the items in the box.
  • 5. WWW.ThesisScientist.com To construct this list component, you first start out with an array of strings, then pass the array to the JList constructor : String[ ] words= { "quick", "brown", "hungry", "wild", ... }; JList wordList= newJList(words); List boxes do not scroll automatically. To make a list box scroll, you must insert it into a scroll pane: JScrollPane scroll Pane=new JScrollPane(wordList); You then add the scroll pane, not the list, into the surrounding panel. By default, the list component displays eight items; use the setVisibleRowCount method to change that value: wordList.setVisibleRowCount(4); // displays 4 items You can set the layout orientation to one of three values:  JList.VERTICAL (the default): arrange all items vertically.  List.VERTICAL_WRAP: start new columns if there are more items than the visible row count.  List.HORIZONTAL_WRAP: start new columns if there are more items than the visible row count, but fill them horizontally. You can also restrict the user to a more limited selection mode with the setSelectionMode method: wordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); / / select one item at a time wordList.setSelectionModel(ListSelectionModel.SINGLE_INTERVAL_SELECTlON); / / select one item or one range of items The basic user interface components send out action events when the user activates them. So u need to add a list selection listener to the list component, and implement the method public void valueChanged(ListSelectionEvent evt) in the listener. When the user selects items, a flurry of list selection events is generated. Once you are notified that an event has happened, you will want to find out what items are currently selected. The getSelectedValues method returns an
  • 6. WWW.ThesisScientist.com array of objects containing all selected items. Cast each array element to a string. Object[ ] values =list.getSelectedValues(); for(Object value : values) do something with (String) value; List Models The most common method for using a list component.  Specify a fixed set of strings for display in the list.  Place the list inside a scroll pane, and  Trap the list selection events. The JList class is responsible for the visual appearance of the data. It actually knows very little about how the data are stored-all it knows is that it can retrieve the data through some object that implements the ListModel interface: public interface ListModel { int getSize () ; Object getElementAt( int i) ; void addListDataListener(ListDataListener l) ; void removeListDataListener(ListDataListener l): } Through this interface, the JList can get a count of elements and retrieve each one of the elements.
  • 7. WWW.ThesisScientist.com Also, the JList object can add itself as a list data listener. That way, if the collection of elements changes, JList gets notified so that it can repaint the list. Adding and removing listeners, has been done in the AbstractListModel class, which we extend. We only need to supply the getSize and getElementAt methods: class WordListModel extends AbstractListModel { public WordListModel(int n) { length = n; } public int getSize() { return (int) Math.pow(26,length); } public Object getElementAt(int n) { II compute nth string } (draw diagram on page no 349) Inserting and Removing Values – You cannot directly edit the collection of list values. Instead, you must access the model and then add or remove elements. Suppose you want to add more values to a list. You can obtain a reference to the model: ListModel model=list.getModel() ; But the ListModel interface also has no methods to insert or remove elements because a list model is that it need not store the elements. Instead, you should construct a DefaultListModel object, fill it with the initial values, and associate it with the list. DefaultListModel model = new DefaultListModel() ; model.addElement("quick"); model.addElement("brown"); …………… JList list = newJList(model) ;
  • 8. WWW.ThesisScientist.com Now you can add or remove values from the model object. The model object then notifies the list of the changes, and the list repaints itself. model.removeElement( "quick"); model.addElement("slow") ; Rendering Values – You can easily represent your list values with any drawing whatsoever. Although the JList class can display strings and icons automatically, you need to install a list cell renderer into the JList object for all custom drawing. A list cell renderer is any class that implements the following interface: interface ListCellRenderer { Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cell HasFocus) ; } This method is called for each cell. It returns a component that paints the cell contents. The component is placed at the appropriate location whenever a cell needs to be rendered. If the rendered image just contains text, an icon, and possibly a change of color, then you can get by with configuring a JLabel.For eg, to show the font name in its own font, we can use the following renderer: class FontCellRenderer extends JLabel implements ListCellRenderer { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, Boolean cellHasFocus) { JLabel label = new JLabel() ; Font font = (Font) value ; setText(font.getFamily()) ; setFont(font) ; setOpaque(true) ; setBackground(isSelected?list.getSelectionBackground():list.getBackground()); setForeground(isSelected?list.getSelectionForeground():list.getForeground()); return this ; } } TREES –
  • 9. WWW.ThesisScientist.com Many tree structures arise in everyday life,such as the hierarchy of countries ,states & cities.The Swing library has a JTree class for this purpose.The JTree class takes care of laying out the tree and processing user requests for expanding and collapsing nodes. A tree is composed of nodes.Every node is either a leaf or it has child nodes.Everynode,with the exception of the root node, has exactly one parent.A tree has exactly one root node. Sometimes you have a collection of trees, each of which has its own root node.Such a collection is called a forest. (draw diagram on page no 358 Fig 6.7 ) Simple Trees - The JTree component follows the model view-controller pattern. You provide a model of the hierarchical data, and the component displays it for you. To construct a JTree, you supply the tree model in the constructor TreeModel model=. . . JTreee tree = new JTree(model) ; To construct a default tree model, you must supply a root node. TreeNode root = . . . DefaultTreeModel model= newDefaultTreeModel(root) ; TreeNode is another interface. you can simply pass the root node to the JTree constructor. Then the tree automatically constructs a default tree model : JTree tree =new JTree(root) ; Editing Trees and Tree Paths - The JTree class has a surprising way of identifying nodes in a tree. It doesnot deal with tree nodes, but with paths of objects,called tree paths. A tree path starts at the root & consists of a sequence of child nodes. (draw diagram on page no 366 Fig 6-18) The JTree class knows nothing about the TreeNode interface.That interace is never used by the TreeModel interface.it is only used by the DefaultTreeModel implementation.You can have other tree models in which the nodes do not implement the TreeNode interface at all.If you use a tree model that manages other types of objects, then those objects may not have getParent & getChild methods. The TreePath class manages a sequence of Object references. A number of JTree methods return Treepath objects.When you have a tree path,you usually just need to Know the terminal node,which you get with the getLastPathComponent method.For eg, to find out the currently selected node in a tree, you use the getSelectionPath method of the JTree class.You get a TreePath object back, from which you can retrieve the actual node. TreePath selectionPath= tree.getSelectionPath() ; DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent() ; If you use the insertNodeInto method of the DefaultTreeModel class, the model class takes care of that. For eg, the following call appends a new node as the last child of the selected node & notifies the tree view.
  • 10. WWW.ThesisScientist.com model.insertNodelnto(newNode, selectedNode , selectedNode.getchildCount()) ; The analogous call removeNodeFromParent removes a node and notifies the view: model.removeNodeFromParent (selectedNode) ; Node Enumeration - Sometimes you need to find a node in a tree by starting at the root & visiting all children until you have found a match.The DefaultMutableTreeNode class has several methods for iterating through nodes. The breadthFirstEnumeration & depthFirstEnumeration methods return enumeration objects whose nextElement method visits all children of the current node,using either a breadth-first or depth-first traversal. Breadth-first enumeration is the easiest to visualize.The tree is traversed in layers.The root is visited first,followed by all of its children, then followed by the grandchildren, and so on.To visualize depth- first enumeration,imagine a rat trapped in a tree-shaped maze. It rushes along the first path until it comes to a leaf.Then, it backtracks and turns around to the next path, and so on. The postOrderTraversal method is a synonym for depthFirstTraversal because the search process visits the childen before visiting the parents. Here is the typical usage pattern: Enumeration breadthFirst = node.breadthFirstEnumeration() ; while (breadthFirst.hasMoreElements()) do something with breadthFirst. nextElement(); Finally,a method, pathFromAncestorEnumeration, finds a path from an ancestor to a given node and then enumerates the nodes along that path. Rendering Nodes- you will often need to change the way in which a tree component draws the nodes.The most common change is to choose different icons for nodes and leaves.Other changes might involve changing the font of the node labels or drawing images at the nodes. All these changes are made possible by installing a new tree cell renderer into the tree.By default, the JTree class uses DefaultTreeCellRenderer objects to draw each node.The DefaultTreeCellRenderer class extends the JLabelclass. The label contains the node icon and the node label. You can customize the display in three ways : 1. You can change the icons, font, and background color used by a DefaultTreeCellRenderer.These settings are used for all nodes in the tree. 2. You can install a renderer that extends the DefaultTreeCellRenderer class and vary the icons, fonts, and background color for each node. 3. You can install a renderer that implements the TreeCellRenderer interface, to draw a custom image for each node. To change the appearance of individual nodes,you install a tree cell renderer.The TreeCellRenderer interface has a single method: Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
  • 11. WWW.ThesisScientist.com Custom Tree Models - The TreeModel interface has only a handful of methods.The first group of methods enables the JTree to find the tree nodes by first getting the root, then the children.The JTree class calls these methods only when the user actually expands a node. Object getRoot () int getChildCount(Object parent) Object getChild(Object parent, int index) TheTreeModel is responsible for telling the JTree how they are connected.The next method of the TreeModel interface is the reverse of getChild: int getlndexOfChild(Object parent,Object child) The tree model tells the JTree which nodes should be displayed as leaves: boolean isLeaf(Object node) If your code changes the tree model, then the tree needs to be notified so that it can redraw itself. The tree adds itself as a TreeModelListener to the model.Thus, the model must support the usual listener management methods: void addTreeModelListener(TreeModelListener l) void removeTreeModelListener(TreeModelListener l) When the model modifies the tree contents, it calls one of the four methods of the TreeModelListener interface: void treeNodesChanged(TreeModelEvent e) void treeNodeslnserted(TreeModelEvent e) void treeNodesRemoved (TreeModelEvent e ) void treeStructureChanged(TreeModelEvent e) The TreeModelEvent object describes the location of the change. Finally if the user edits a tree node, your model is called with the change. Void valueForPathChanged(TreePath path ,Object newValue) If you don’t allow editing, this method is never called. Progress Indicators - A JProgressBar is a Swing component that indicates progress. A ProgressMonitor is a dialog box that contains a progress bar. A ProqressMonitorInputStream displays a progress monitor dialog box while the stream is read. Progress Bars A progressbar is a simple component-just a rectangle that is partially filled with color to indicate the progress of an operation. By default, progress is indicated by a string “n% “. You construct a progress bar much as you construct a slider, by supplying the minimum and maximum value and an optional orientation: progressBar = newJProgressBar(0, 1000); progressBar= newJProgressBar(SwingConstants. VERTICAL, 0, 1000);
  • 12. WWW.ThesisScientist.com You can also set the minimum and maximum with the setMinimum and setMaximum methods. Unlike a slider,the progress bar cannot be adjusted by the user. Your program needs to call setValueto update it. If you call progressBar. setStringPainted(true); the progress bar computes the completion percentage and displays a string ”n% “ .If you want to show a different string, you can supply it with the setString method: if (progressBar.getValue() > 900) progressBar.setString(" AlmostDone "); Progress Monitor A ProgressMonitor is a complete dialog box that contains a progress bar.The dialog box contains a Cancel button.If you click it,the monitor dialog box is closed.In addition,your program can query whether the user has canceled the dialog box & terminate the monitored action. You construct a progress monitor by supplying the following:  The parent component over which the dialog box should pop up.  An object that is displayed on the dialog box.  An optional note to display below the object.  The minimum and maximum values. . As you update the progress value, you should also call the isCanceled method to see if the program user has clicked the Cancel button. When the monitored activity has concluded, call the close method to dismiss the dialog box.You can reuse the same dialog box by calling start again. There are two conditions for termination. The activity might have completed, or the user might have canceled it. In each of these cases, we close down :  The timer that monitored the activity.  The progress dialog box.  The activity itself (by interrupting the thread). Component Organizers – These include the split pane, a mechanism for splitting an area into multiple parts whose boundaries canbe adjusted,the tabbed pane,which uses tab dividers to allow a user to flip through multiple panels,& the desktop pane.,which can be used to implement applications that display multiple internal frames.