Professional Documents
Culture Documents
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.
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.
Constructor Description
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.
import javax.swing.tree.*;
class SimpleTree
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
public SimpleTreeFrame()
setTitle("simple tree");
setSize(400,400);
root.add(country);
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);
state.add(city);
country=new DefaultMutableTreeNode("germany");
root.add(country);
state=new DefaultMutableTreeNode("schlewing");
country.add(state);
city=new DefaultMutableTreeNode("keil");
state.add(city);
add(new JScrollPane(tree));
}
Java JTable
The JTable class is used to display data in tabular form. It is composed of rows and
columns.
Constructor Description
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.
Constructor Description
JProgressBar(int min, It is used to create a horizontal progress bar with the specified
int max) minimum and maximum value.
Method Description
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.*;
JProgressBar jb;
int i=0,num=0;
Progress(){
jb.setBounds(40,40,160,30);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(250,150);
setLayout(null);
while(i<=2000){
jb.setValue(i);
i=i+20;
try{Thread.sleep(150);}catch(Exception e){}
m.setVisible(true);
m.iterate();