0% found this document useful (0 votes)
30 views35 pages

Swing Components - Updated JRadioButton, Combobox and Jscrollpane, Tabbedpane, Tree

Uploaded by

sayyedanadil04
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)
30 views35 pages

Swing Components - Updated JRadioButton, Combobox and Jscrollpane, Tabbedpane, Tree

Uploaded by

sayyedanadil04
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/ 35

JComboBox

Swing provides a combo box


through the JComboBox class,
which extends JComponent.
It is a combination of a text field
and a drop-down list
 A combo box normally displays
one entry. However, it can also
display a drop-down list that allows
a user to select a different entry.
Constructors of
JComboBox
JComboBox's constructors are
shown here:
JComboBox( ): Creates a JComboBox
with a default data model.
JComboBox(Vector v): Creates a
JComboBox that contains the
elements in the specified Vector.
JComboBox Method
Items are added to the list of
choices via the addItem( )
method, whose signature is
shown here:
void addItem(Object obj)
Program Using JComboBox
import java.awt.*; /*<applet
import javax.swing.*; code=combobox
public class combobox width=200 height=200>
extends JApplet
{ </applet >*/
public void init()
{
Container
c=getContentPane();
setLayout(new
FlowLayout());
JComboBox jcb = new
JComboBox();
jcb.addItem("Vanilla");
jcb.addItem("Chocolate");
jcb.addItem("Strawberry");
Program using Vector
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class combobox_string1
extends JApplet
{
public void init()
{
String
s[]={"mumbai","pune","nagpur"};
Container co=getContentPane();
JComboBox c=new JComboBox(s);
co.setLayout(new FlowLayout());
co.add(c);
}
}
/*<applet code=combobox_string1
width=200 height=200>
</applet>*/
Program Using
JComboBox(Vector)
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class combobox_vec1 extends
JApplet
{
public void init()
{
Vector v=new Vector();
v.addElement("Mumbai");
v.addElement("Pune");
v.addElement("Nagpur");
Container co=getContentPane();
JComboBox c=new JComboBox(v);
co.setLayout(new FlowLayout());
co.add(c);
}
}
/*<applet code=combobox_vec1
width=200 height=200>
</applet>*/
JScrollPane
A scroll pane is a component
that presents a rectangular area
in which a component may be
viewed.
Horizontal and/or vertical scroll
bars may be provided if
necessary.
Scroll panes are implemented in
Swing by the JScrollPane class,
which extends JComponent.
Program using command line
argument
import java.awt.*;
import javax.swing.*;
co.add(c);
import java.util.*;
class combobox_vec extends
v1.setSize(200,20
JFrame 0);
{
public static void main(String ar[]) v1.setVisible(true)
{combobox_vec v1=new
combobox_vec();
;
Container
co=v1.getContentPane();
}
co.setLayout(new FlowLayout());
Vector v=new Vector();
}
for(int i=0;i<ar.length;i++)
v.addElement(ar[i]);
JComboBox c=new JComboBox(v);
The sequential steps to use a scroll pane in
an applet are:

 Create a JComponent object.


 Create a JScrollPane object. (The
arguments to the constructor specify the
Component and the policies for vertical
and horizontal scroll bars)
 Add the scroll pane to the content pane of
the applet.
Constructors of
JScrollPane
 JScrollPane(Component comp)
 JScrollPane(int vsb, int hsb)
 JScrollPane(Component comp, int vsb, int hsb)

Here, comp is the component to be added to the scroll

pane. vsb and hsb are int constants that define when

vertical and horizontal scroll bars for this scroll pane are

Shown. These constants are defined by the

ScrollPaneConstants interface.
Constant Description

 HORIZONTAL_SCROLLBA  Always provide


R_ALWAYS horizontal scroll bar

 HORIZONTAL_SCROLLBA  Provide horizontal


R_AS_NEEDED scroll bar, if needed
 VERTICAL_SCROLLBAR_A  Always provide
LWAYS vertical scroll bar
 VERTICAL_SCROLLBAR_A  Provide vertical scroll
S_NEEDED bar, if needed

JScrollBar Constants
Program Using JScrollPane
import java.awt.*; for(int j = 0; j < 20; j++) {
import javax.swing.*;
jp.add(new JButton("Button "
import java.applet.*;
+ b));
/*<applet code="jscroll" width=300
height=250> ++b;
</applet>*/ }
public class jscroll extends JApplet }
{
// Add panel to a scroll pane
public void init()
int v =
{
ScrollPaneConstants.VERTICAL_S
Container contentPane = CROLLBAR_ALWAYS;
getContentPane();
int h =
// Add 400 buttons to a panel
ScrollPaneConstants.HORIZONTA
JPanel jp = new JPanel(); L_SCROLLBAR_AS_NEEDED;
jp.setLayout(new GridLayout(20,
20));
JScrollPane jsp = new
JScrollPane(jp, v, h);
int b = 0;
// Add scroll pane to the content
for(int i = 0; i < 20; i++) {
pane
Output
Program Using JScrollPane
import java.awt.*;
import javax.swing.*;
import java.applet.*;
/*<applet code="jscroll2" width=300 height=250>
</applet>*/
public class jscroll2 extends JApplet
{
public void init()
{
Container co = getContentPane();
JButton b1=new JButton("ok");
JButton b2=new JButton("exit");
JPanel jp =new JPanel();
jp.add(b1);
jp.add(b2);
//Add panel to a scroll pane
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jp, v, h);
// Add scroll pane to the content pane
co.add(jsp);}}
JTable
A table is a component that displays
rows and columns of data. We can
drag the cursor on column boundaries
to resize columns. We can also drag a
column to a new position. Tables are
implemented by the JTable class,
which extends JComponent.
Constructor of JTable

JTable(Object data[ ][ ], Object colHeads[ ])

Here, data is a two-dimensional array of the


information to be presented, and colHeads is
a one-dimensional array with the column
headings.
Here are the steps for using a table in an
applet:

1. Create a JTable object.


2. Create a JScrollPane object. (The
arguments to the constructor specify
the table
and the policies for vertical and
horizontal scroll bars.)
3. Add the table to the scroll pane.
4. Add the scroll pane to the content
pane of the applet.
Program Using JTable
// Initialize data
import java.awt.*;
final Object[][] data = {
import javax.swing.*; { "Ram", "4567", "Mumbai" },
/*<applet code="jtable" { "Ken", "7566", "Delhi" },
width=400 height=200> { "Shyam", "5634", "Nagpur" },
{ "Melanie", "7345", "Pune" }
</applet>*/
};
public class jtable extends // Create the table
JApplet JTable table = new JTable(data,
{ colHeads);
// Add table to a scroll pane
public void init()
int v =
{ ScrollPaneConstants.VERTICAL_SCROLL
BAR_AS_NEEDED;
// Get content pane
int h =
Container contentPane = ScrollPaneConstants.HORIZONTAL_SCR
getContentPane(); OLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(table,
// Set layout manager v, h);
//contentPane.setLayout(new // Add scroll pane to the content pane
BorderLayout()); contentPane.add(jsp);
// Initialize column headings }
}
final String[] colHeads =
{ "Name", "Phone", "Address" };
Output JTable
JTabbedPane
A tabbed pane is a component that
appears as a group of folders in a file
cabinet. Each folder has a title. When a
user selects a folder, its contents become
visible. Only one of the folders may be
selected at a time. Tabbed panes are
commonly used for setting configuration
options.
JTabbedPane
Tabbed panes are encapsulated
by the JTabbedPane class, which
extends JComponent. We will
use its default constructor.
Tabs are defined via the
following method:

void addTab(String str, Component


comp)
Here, str is the title for the tab, and comp
is the component that should be added to
the tab.
Typically, a JPanel or a subclass of it is
added.
The general procedure to use a tabbed pane in an applet is outlined here:

1. Create a JTabbedPane object.

2. Call addTab( ) to add a tab to the pane. (The


arguments to this method define

the title of the tab and the component it contains.)

3. Repeat step 2 for each tab.

4. Add the tabbed pane to the content pane of the


applet.
Program Using JTabbedPane
import javax.swing.*; {
/*<applet code="panedemo" JCheckBox cb1 = new
width=400 height=100> JCheckBox("Red");
</applet>*/ add(cb1);
public class panedemo extends JCheckBox cb2 = new
JApplet JCheckBox("Green");
{ add(cb2);
public void init() JCheckBox cb3 = new
{ JCheckBox("Blue");
JTabbedPane jtp = new add(cb3);
JTabbedPane(); }}
jtp.addTab("Colors", new class FlavorsPanel extends JPanel
ColorsPanel()); {public FlavorsPanel()
jtp.addTab("Flavors", new {
FlavorsPanel());
JComboBox jcb = new
getContentPane().add(jtp); JComboBox();
} jcb.addItem("Vanilla");
} jcb.addItem("Chocolate");
class ColorsPanel extends JPanel jcb.addItem("Strawberry");
{
Program Using
JTabbedPane
JTree

A tree is a component that presents a


hierarchical view of data. A user has the
ability to expand or collapse individual
subtrees in this display. Trees are
implemented in Swing by the JTree
class, which extends JComponent.
Some of its constructors are shown here:
Constructors of JTree
JTree(Hashtable ht)
The first form creates a tree in which
each element of the hash table ht is
a child node.
JTree(Object obj[ ])
Each element of the array obj is a
child node
Constructors of JTree
JTree(TreeNode tn)
The tree node tn is the root of the
tree
JTree(Vector v)
It uses the elements of vector v as
child nodes.
.

The DefaultMutableTreeNode
class implements the
MutableTreeNode interface It
represents a node in a tree.
JTree
One of its constructors is shown
here:

DefaultMutableTreeNode(Objec
t obj)
Here, obj is the object to be
enclosed in this tree node.
JTree
To create a hierarchy of tree
nodes, the add( ) method of
DefaultMutableTreeNode can
be used. Its signature is shown
here:

void add(MutableTreeNode child)


here, child is a mutable tree node
that is to be added as a child to
the current node.
Here are the steps that we should
follow to use a tree in an applet:

1.Create JTree object


2.Create JScrollPane object
3.Add Tree to this Scroll Pane
4.Add ScrollPane to content Pane
of the Applet.
Output
DefaultMutableTreeNode g = new
import java.awt.*; DefaultMutableTreeNode("green");
import java.awt.event.*; a1.add(g);
import javax.swing.*;
DefaultMutableTreeNode r = new
import javax.swing.tree.*; DefaultMutableTreeNode("red");
/*<applet code="jtree1" width=400
a1.add(r);
height=200>
</applet>*/ DefaultMutableTreeNode a2 = new
DefaultMutableTreeNode("font");
public class jtree1 extends JApplet
{JTree tree;
top.add(a2);
public void init() DefaultMutableTreeNode lg = new
DefaultMutableTreeNode("light
{Container contentPane =
getContentPane(); green");
// Create top node of tree g.add(lg);
DefaultMutableTreeNode top = new tree = new JTree(top);
DefaultMutableTreeNode("Options"); int v =
// Create subtree of "A" ScrollPaneConstants.VERTICAL_SCROL
DefaultMutableTreeNode a = new LBAR_AS_NEEDED;
DefaultMutableTreeNode("style");
int h =
top.add(a); ScrollPaneConstants.HORIZONTAL_SC
DefaultMutableTreeNode a1 = new ROLLBAR_AS_NEEDED;
DefaultMutableTreeNode("color");
JScrollPane jsp = new JScrollPane(tree,
a.add(a1); v, h);

You might also like