0% found this document useful (0 votes)
6 views39 pages

Swing Advance Components

The document provides an overview of the Swing API, which is used for creating Java-based GUI applications, detailing various components such as JProgressBar, JPanel, JScrollPane, JTable, JTabbedPane, JTree, and event handling mechanisms. It includes constructors and methods for each component, along with example code snippets demonstrating their usage. Additionally, it explains the delegation event model, event sources, and event listeners in Swing applications.

Uploaded by

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

Swing Advance Components

The document provides an overview of the Swing API, which is used for creating Java-based GUI applications, detailing various components such as JProgressBar, JPanel, JScrollPane, JTable, JTabbedPane, JTree, and event handling mechanisms. It includes constructors and methods for each component, along with example code snippets demonstrating their usage. Additionally, it explains the delegation event model, event sources, and event listeners in Swing applications.

Uploaded by

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

Introduction to Swing

Swing API is set of extensible GUI


Components to create JAVA based Front End/
GUI Applications. It is build upon top of AWT
API and acts as replacement of AWT API as it
has almost every control corresponding to
AWT controls.
JProgressBar
The class JProgressBar is a component which
visually displays the progress of some task.
Constructors of JProgessBar
 JProgressBar()
Creates a horizontal progress bar that displays a
border but no progress string.
 JProgressBar(int orient)
Creates a progress bar with the specified
orientation, which can be either SwingConstants.
VERTICAL or SwingConstants.HORIZONTAL.
 JProgressBar(int min, int max)
Creates a horizontal progress bar with the
specified minimum and maximum values.
 JProgressBar(int orient, int min, int max)
Creates a progress bar using the specified
orientation, minimum and maximum values.
Methods used with JProgressBar
• void setStringPainted(boolean b)
– determines whether the progress bar should display a
progress string.
• int getMaximum()
– Returns the progress bar's maximum value
• int getMinimum()
– Returns the progress bar's minimum value from the
BoundedRangeModel.
• int getOrientation()
– Returns SwingConstants.VERTICAL or
SwingConstants.HORIZONTAL, depending on the
orientation of the progress bar.
import javax.swing.*; public void iterate()
public class MyProgress extends Jframe {
{ while(i<=2000)
JProgressBar jb; {
int i=0,num=0; jb.setValue(i);
i=i+20;
MyProgress() try
{ {
jb=new JProgressBar(0,2000); Thread.sl
jb.setBounds(40,40,200,30); eep(150);
}
jb.setValue(0); catch(Excepti
jb.setStringPainted(true); on e){}
}
add(jb); }
setSize(400,400); public static void
setLayout(null); main(String[] args)
} {
setDefaultCloseOperation(JFrame.EXIT_ON_ MyProgress m=new MyProgress();
CLOSE); m.setVisible(true);
JPanel
A panel is a swing container which is used
for grouping components which is later on
added to the frame. In java swing panel is
implemented by JPanel class.
Constructors of JPanel
 JPanel()
Creates a panel.
 JPanel(LayoutManager)
The LayoutManager parameter provides a
layout manager for the new panel. By default,
a panel uses a FlowLayout to lay out its
components.
import javax.swing.*;
import java.io.*;
class B
{
public static void
main(String arg[])
{
JFrame f=new JFrame("this is my new frame");
JPanel p=new JPanel();
f.add(p);
f.setSize(200,200);
f.setVisible(true);
f.setDefaultCloseO
peration(JFrame.E
XIT_ON_CLOSE);
}
JScrollPane

A JScrollPane provides a scrollable view of a


component. When screen real
limited, use a scroll pane estate is
to display
component that is large or one whose size can
change dynamically. i.e. It represents a a
scrollpane which is a rectangular area in which
a component may be viewed.
Constructors of JScrollPane
JScrollPane()
Create a scroll pane
JScrollPane(Component com)
com is component which is added to scroll
pane.
JScrollPane(int vsb, int hsb)
vsb and hsb are int constants for controlling
vertical and horizontal scrolling.
JScrollPane(Component com, int vsb, int hsb)
int constants vsb and hsb can have the values:

• VERTICAL_SCROLLBAR_ALWAYS
– Vertical scroll bar is always provided.
• HORIZONTAL_SCROLLBAR_ALWAYS
– Horizontal scroll bar is always provided.
• VERTICAL_SCROLLBAR_AS_NEEDED
– Vertical scroll bar is provided as per the need.
• HORIZONTAL_SCROLLBAR_AS_NEEDED
– Horizontal scroll bar is provided as per the need.
import javax.swing.*;
import java.awt.event.*;
public class scroll {
public static void
main(String[] args) {
String s="Welcome to Java Swing programming!!!!! This shows the working of
JScrollPane.....";
JTextArea t=new JTextArea(s,10,10);
JFrame f=new JFrame("Frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
int
v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAY
S;
int
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS
_NEEDED;
JScrollPane sp=new JScrollPane(t,v,h);
p.add(sp);
f.add(p);
f.setSize(200,200);
JTable
The JTable class is used to display the data on
two dimensional tables of cells.

Constructors of JTable class:


 JTable()
creates a table with empty cells.
 JTable(Object[][] rows, Object[] columns)
creates a table with the specified data.
import javax.swing.*;
public class MyJTable extends JFrame {
MyJTable()
{
String data[][]={
{"101","Umesh","670000"},
{"102","Vijay","780000"},
{"101","Rahul","700000"}};
String column[]={"ID","NAME","SALARY"};

JTable jt=new JTable(data,column);


jt.setBounds(30,40,200,300);

JScrollPane sp=new JScrollPane(jt);


add(sp);

setSize(300,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyJTable();
}
}
JTabbedPane
JTabbedPane is a container component that
lets the user switch between pages by clicking
on a tab.
Constructors for JTabbedPane
 JTabbedPane()
Creates an empty TabbedPane with a default tab
placement of JTabbedPane.TOP.
 JTabbedPane(int tabPlacement)
Creates an empty TabbedPane with the specified
tab placement of either: JTabbedPane.TOP,
JTabbedPane.BOTTOM, JTabbedPane.LEFT, or
JTabbedPane.RIGHT.
 JTabbedPane(int tabPlacement, int tabLayoutPolicy)
Creates an empty TabbedPane with the specified
tab placement and tab layout policy.
import javax.swing.*;
/* <applet code="exp43" width=400 height=100> </applet> */

public class exp43 extends JApplet


{
public void init()
{
JTabbedPane exp43 = new JTabbedPane();
exp43.addTab("Cities", new CitiesPanel());
exp43.addTab("Colors", new ColorsPanel());
exp43.addTab("Flavors", new FlavorsPanel());
add(exp43);
}
}
class CitiesPanel extends JPanel
{
public CitiesPanel()
{
JButton b1 = new JButton("Kolhapur");
add(b1);
JButton b2 = new JButton("Karad");
add(b2);
JButton b3 = new JButton("Satara");
add(b3);
JButton b4 = new JButton("Pune");
add(b4);
}
}
class ColorsPanel extends JPanel
{
public ColorsPanel()
{
JCheckBox cb1 = new JCheckBox("Red");
add(cb1);
JCheckBox cb2 = new JCheckBox("Green"); add(cb2);
JCheckBox cb3 = new JCheckBox("Blue"); add(cb3);
}
}

class FlavorsPanel extends JPanel


{
public FlavorsPanel()
{
JComboBox jcb = new JComboBox();
jcb.addItem("Vanilla");
jcb.addItem("Chocolate");
jcb.addItem("Strawberry");
add(jcb);
}
JTree
JTree class is a powerful Swing component for
displaying tree-structured data.
A JTree object does not actually contain your data; it
simply provides a view of the data. Like any non-
trivial Swing component, the tree gets data by
querying its data model. Here is a picture of a tree:
 Each row displayed by the tree contains
exactly one item of data, which is called
a node.
 Every tree has a root node from which
all nodes descend.
 A node can either have children or not.
 We refer to nodes that can have children
as branch nodes. Nodes that can not have
children are leaf nodes.
 Branch nodes can have any number of
children.
 A specific node in a tree can be identified
either by a TreePath, an object that
encapsulates information about the node, or
by its display row, where each row in the
display area displays one node.
 An expanded node is a non-leaf node that will
display its children when all its ancestors are
expanded.
 A collapsed node is one which hides them.
 A hidden node is one which is under a
collapsed ancestor.
Constructors Of JTree
• JTree(Hashtable ht)
– Returns a JTree created from a Hashtable which does
not display with root. Each element of hashtable is
• a childnode.
JTree(Object[] value)
– Returns a JTree with each element of the specified array as
the child of a new root node which is not displayed.
• Each element of the array object is childnode.
JTree(TreeNode root)
– Returns a JTree with the specified TreeNode as its root, which
• displays the root node. Treenode tn is the root of the tree.
JTree(Vector v)
– Returns a JTree with each element of the specified Vector as
the child of a new root node which is not displayed.
Elements of vector v is the childnode
• It is possible to obtain a reference to the
parent node.
• The ‘MutableTreeNode’ interface extends
‘TreeNode’, which is an interface that
declares methods that obtain information
about a tree node.
• The ‘DefaultMutableTreeNode’ class
implements the ‘MutableTreeNode’
interface. It represents a node in a tree.
Methods used with JTree
DefaultMutableTreeNode(object obj)
where ‘obj’ is the object to be enclosed in this
tree node.
void add(MutableTreeNode child)
This method can be used to create the hierarchy
of nodes .where ‘child’ is the mutable treenode
this is to be added as a child to the current node.
TreePath getPathForLocation(int x, int y)
It is used to translate a mouse click on a point of
tree to a tree path. Where, x and y are
coordinates of mouse click.
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class tree
{
public static void
main(String[] args)
{
wood o=new wood();
o.wod();
}
}
class wood extends JFrame
{
public void wod()
{
JFrame f=new JFrame();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("Asia");
DefaultMutableTreeNode country=new
DefaultMutableTreeNode("India");
DefaultMutableTreeNode state=new
DefaultMutableTreeNode("Madhya
Pradesh");
DefaultMutableTreeNode city=new DefaultMutableTreeNode("Gwalior");
root.add(country);
country.add(state);
state.add(city);
JTree tree=new JTree(root);
f.add(tree);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300,300);
f.setVisible(true);
}
ToolTip
Tooltip text is normally one line long. The
setToolTipText() method from JComponent
used to create a tooltip.

public void setToolTipText(String text)


-Registers the text to display in a tool tip.
The text displays when the cursor lingers over
the component.
import javax.swing.JButton;
import javax.swing.JFrame;
public class JButtonWithTooltip extends Jframe
{
public JButtonWithTooltip()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b = new JButton("Test");
b.setToolTipText("Help text for the button");
getContentPane().add(b, "Center");
pack();
}
public static void main(String[] args)
{
new JButtonWithTooltip().setVisible(true);
}
}
Event Handling
• The Delegation Event Model: The delegation
event model, which defines standard and
consistent mechanisms to generate and
process events.
• Its concept is quite simple: a source
generates an event and sends it to one or
more listeners. In this scheme, the listener
simply waits until it receives an event. Once
received, the listener processes the event
and then returns
Event Sources:
• A source is an object that generates an event.
• . Sources may generate more than one type of event.
• A source must register listeners in order for the listeners to
receive notifications about a specific type of event.
• Here is the general form:
public void addTypeListener(TypeListener el)

• Here, Type is the name of the event and el is a reference to


the event listener.
• A source must also provide a method that allows a listener
to unregister an interest in a specific type of event.
• The general form of such a method is this:
public void removeTypeListener(TypeListener el)
• Here, Type is the name of the event and el is a reference to
the event listener.
Event Listeners:

A listener is an object that is notified when an


event occurs.
It has two major requirements.
• it must have been registered with one or more
sources to receive notifications about specific
types of events.

• it must implement methods to receive and


process these notifications. The methods that
receive and process events are defined in a set
of interfaces found in java.awt.event.

You might also like