import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class treeevents extends JApplet
{
JTree tree;
JTextField jft;
public void init()
{
Container c=getContentPane();
c.setLayout(new BorderLayout());
DefaultMutableTreeNode top=new DefaultMutableTreeNode("Country");
DefaultMutableTreeNode a=new DefaultMutableTreeNode("INDIA");
top.add(a);
DefaultMutableTreeNode aa=new DefaultMutableTreeNode("SriLanka");
top.add(aa);
DefaultMutableTreeNode b=new DefaultMutableTreeNode("USA");
top.add(b);
DefaultMutableTreeNode bb=new DefaultMutableTreeNode("UK");
top.add(bb);
DefaultMutableTreeNode a1=new DefaultMutableTreeNode("Chennai");
a.add(a1);
DefaultMutableTreeNode a2=new DefaultMutableTreeNode("Delhi");
a.add(a2);
DefaultMutableTreeNode a3=new DefaultMutableTreeNode("Mumbai");
a.add(a3);
DefaultMutableTreeNode aa1=new DefaultMutableTreeNode("columbu");
aa.add(aa1);
DefaultMutableTreeNode b1=new DefaultMutableTreeNode("NewYork");
b.add(b1);
DefaultMutableTreeNode b2=new DefaultMutableTreeNode("Washington");
b.add(b2);
DefaultMutableTreeNode b3=new DefaultMutableTreeNode("Canada");
b.add(b3);
DefaultMutableTreeNode bb1=new DefaultMutableTreeNode("London");
bb.add(bb1);
DefaultMutableTreeNode abc=new DefaultMutableTreeNode("ABC");
bb1.add(abc);
DefaultMutableTreeNode bb2=new DefaultMutableTreeNode("Sheifield");
bb.add(bb2);
tree=new JTree(top);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(tree,v,h);
c.add(jsp,BorderLayout.CENTER);
jft=new JTextField(" ",20);
c.add(jft,BorderLayout.SOUTH);
tree.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
doMouseClicked(me);
}
}
);
}
void doMouseClicked(MouseEvent me)
{
TreePath tp=tree.getPathForLocation(me.getX(),me.getY());
if(tp!=null)
jft.setText(tp.toString());
else
jft.setText("");
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class treeevents extends JApplet
{
JTree tree;
JTextField jft;
public void init()
{
Container c=getContentPane();
c.setLayout(new BorderLayout());
DefaultMutableTreeNode top=new DefaultMutableTreeNode("Country");
DefaultMutableTreeNode a=new DefaultMutableTreeNode("INDIA");
top.add(a);
DefaultMutableTreeNode aa=new DefaultMutableTreeNode("SriLanka");
top.add(aa);
DefaultMutableTreeNode b=new DefaultMutableTreeNode("USA");
top.add(b);
DefaultMutableTreeNode bb=new DefaultMutableTreeNode("UK");
top.add(bb);
DefaultMutableTreeNode a1=new DefaultMutableTreeNode("Chennai");
a.add(a1);
DefaultMutableTreeNode a2=new DefaultMutableTreeNode("Delhi");
a.add(a2);
DefaultMutableTreeNode a3=new DefaultMutableTreeNode("Mumbai");
a.add(a3);
DefaultMutableTreeNode aa1=new DefaultMutableTreeNode("columbu");
aa.add(aa1);
DefaultMutableTreeNode b1=new DefaultMutableTreeNode("NewYork");
b.add(b1);
DefaultMutableTreeNode b2=new DefaultMutableTreeNode("Washington");
b.add(b2);
DefaultMutableTreeNode b3=new DefaultMutableTreeNode("Canada");
b.add(b3);
DefaultMutableTreeNode bb1=new DefaultMutableTreeNode("London");
bb.add(bb1);
DefaultMutableTreeNode abc=new DefaultMutableTreeNode("ABC");
bb1.add(abc);
DefaultMutableTreeNode bb2=new DefaultMutableTreeNode("Sheifield");
bb.add(bb2);
tree=new JTree(top);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(tree,v,h);
c.add(jsp,BorderLayout.CENTER);
jft=new JTextField(" ",20);
c.add(jft,BorderLayout.SOUTH);
tree.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
doMouseClicked(me);
}
}
);
}
void doMouseClicked(MouseEvent me)
{
TreePath tp=tree.getPathForLocation(me.getX(),me.getY());
if(tp!=null)
jft.setText(tp.toString());
else
jft.setText("");
}
}
No comments:
Post a Comment