You are on page 1of 9

JTREE

1. Introduction:
JTree is a Swing component with which we can display hierarchical data. JTree is quite a complex component. A
JTree has a 'root node' which is the top-most parent for all nodes in the tree. A node is an item in a tree. A node
can have many children nodes. These children nodes themselves can have further children nodes. If a node doesn't
have any children node, it is called a leaf node.Let's see the declaration for javax.swing.JTree class.

JTree class declaration


Let's see the declaration for javax.swing.JTree class.

1. public class JTree extends JComponent implements Scrollable, Accessible

2. Developing a Simple JTree:

Let us now attempt to build a simple JTree. Let us say we want to display the list of vegetables and fruits
hierarchically.

The node is represented in Swing API as TreeNode which is an interface. The interface MutableTreeNode extends
this interface which represents a mutable node. Swing API provides an implementation of this interface in the
form of DefaultMutableTreeNode class.

1. public class JTree extends JComponent implements Scrollable, Accessible

Commonly used Constructors:

Constructor Description

JTree() Creates a JTree with a sample model.

JTree(Object[] Creates a JTree with every element of the specified array as the
value) child of a new root node.
JTree(TreeNode Creates a JTree with the specified TreeNode as its root, which
root) displays the root node.

/* hierarchy of countries ,states & cities*/


import javax.swing.*;

import javax.swing.tree.*;

class SimpleTree

public static void main(String[] args) {

JFrame frame=new SimpleTreeFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

class SimpleTreeFrame extends JFrame

public SimpleTreeFrame()

setTitle("simple tree");

setSize(400,400);

DefaultMutableTreeNode root=new DefaultMutableTreeNode("World");

DefaultMutableTreeNode country=new DefaultMutableTreeNode("USA");

root.add(country);

DefaultMutableTreeNode state=new DefaultMutableTreeNode("California");

country.add(state);
DefaultMutableTreeNode city=new DefaultMutableTreeNode("San jose");

state.add(city);

city=new DefaultMutableTreeNode("Cupertino");

state.add(city);

state=new DefaultMutableTreeNode("Michigen");

country.add(state);

city=new DefaultMutableTreeNode("Ann Arbor");

state.add(city);

country=new DefaultMutableTreeNode("germany");

root.add(country);

state=new DefaultMutableTreeNode("schlewing");

country.add(state);

city=new DefaultMutableTreeNode("keil");

state.add(city);

JTree tree=new JTree(root);

add(new JScrollPane(tree));

}
Java JTable
The JTable class is used to display data in tabular form. It is composed of rows and
columns.

JTable class declaration


Let's see the declaration for javax.swing.JTable class.

Commonly used Constructors:

Constructor Description

JTable() Creates a table with empty cells.

JTable(Object[][] rows, Object[] Creates a table with the specified


columns) data.

a program to create table using swings.

package table;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Table {
public static void main(String[] args) {
JFrame f=new PFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);// TODO code application logic here
}
}
class PFrame extends JFrame
{
Object[][] data={{"mercury",2440,0},
{"venus",6052,0},
{"earth",6350,0}};
String[] cols={"planet","radius","moons"};
JTable t;
public PFrame()
{
setTitle("PLANET TABLE");
setSize(400,200);
t=new JTable(cells,cols);
JScrollPane s=new JScrollPane(t);
add(t,BorderLayout.CENTER);
JButton b1=new JButton("print");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try{t.print();}
catch(Exception e1)
{
System.out.println(e1);
}
}
});
add(b1,BorderLayout.SOUTH);
}}
Output :
JProgressBar
A progress bar is a widget that displays progress of a lengthy task, for instance file download or transfer. To
create a progress bar in swing you use the JProgressbar class. With JProgressBar you can either
create vertical or horizontal progress bar.

It inherits JComponent class.

JProgressBar class declaration


Let's see the declaration for javax.swing.JProgressBar class.

1. public class JProgressBar extends JComponent implements SwingConstants, Accessible

Commonly used Constructors:

Constructor Description

JProgressBar() It is used to create a horizontal progress bar but no string text.

JProgressBar(int min, It is used to create a horizontal progress bar with the specified
int max) minimum and maximum value.

JProgressBar(int It is used to create a progress bar with the specified orientation, it


orient) can be either Vertical or Horizontal by using
SwingConstants.VERTICAL and SwingConstants.HORIZONTAL
constants.

JProgressBar(int It is used to create a progress bar with the specified orientation,


orient, int min, int minimum and maximum value.
max)
Commonly used Methods:

Method Description

void It is used to determine whether string should be displayed.


setStringPainted(boolean b)

void setString(String s) It is used to set value to the progress string.

void setOrientation(int It is used to set the orientation, it may be either vertical or horizontal by using
orientation) SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants.

void setValue(int value) It is used to set the current value on the progress bar.

import javax.swing.*;

public class Progress extends JFrame{

JProgressBar jb;

int i=0,num=0;

Progress(){

jb=new JProgressBar(SwingConstants. VERTICAL,0,2000);

jb.setBounds(40,40,160,30);

jb.setValue(0);

jb.setStringPainted(true);

add(jb);

setSize(250,150);

setLayout(null);

public void iterate(){

while(i<=2000){
jb.setValue(i);

i=i+20;

try{Thread.sleep(150);}catch(Exception e){}

public static void main(String[] args) {

Progress m=new Progress();

m.setVisible(true);

m.iterate();

You might also like