Featured Post

Friday, February 28, 2020

Swing program using JTree



import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
public class JTreedemo extends JApplet
{
  public void init()
  {
   try{
   SwingUtilities.invokeAndWait(new Runnable(){
   public void run()
   {
     makeGUI();
   }
   });
   }
   catch(Exception e)
   {
     System.out.println("cannot create"+e);
   }
 }
 private void makeGUI()
 {
   DefaultMutableTreeNode top=new DefaultMutableTreeNode("options");
   DefaultMutableTreeNode a=new DefaultMutableTreeNode("A");
   top.add(a);
   DefaultMutableTreeNode a1=new DefaultMutableTreeNode("A1");
   a.add(a1);
   DefaultMutableTreeNode a2=new DefaultMutableTreeNode("A2");
   a.add(a2);
   DefaultMutableTreeNode b=new DefaultMutableTreeNode("B");
   top.add(b);
   DefaultMutableTreeNode b1=new DefaultMutableTreeNode("B1");
   b.add(b1);
   DefaultMutableTreeNode b2=new DefaultMutableTreeNode("B2");
   b.add(b2);
   JTree tree=new JTree(top);
   add(tree);
 }
}
  
   
*****************************************************************
<html>
<body>
<applet code="JTreedemo.class" width=200 height=300>
</applet>
</body>

</html>

Output:



   

No comments:

Post a Comment