import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.util.*;
/*
<
applet code = "JTreeDemo2" width=4
00 height=200>
</applet>
*/
public class JTreeDemo2 extends JApplet
{
JTree tree;
JLabel jlabel;
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
makeGUI();
}
}
);
}
catch (Exception e)
{
System.out.println("can't create coz of" + e);
}
}
private void makeGUI()
{
DefaultMutableTreeNode grandparent =null;
Scanner sc=new Scanner(System.in);
System.out.println ("Enter a string ");
String s=sc.nextLine();
int l;
int i;
DefaultMutableTreeNode parent1 = null;
char a[]=new char[s.length()/2];
for (i=1,l=0;i<s.length();i+=2,l++)
{
a[l]=s.charAt(i);
System.out.println ("In if "+a[l]);
}
char p=a[0];
char p1=p;
for(i=0;i<a.length;i++)
{
if(p==a[i])
{
p=a[i];
}
if(p!=a[i])
{
p1=a[i];
}
grandparent = new DefaultMutableTreeNode(p1);
parent1 = new DefaultMutableTreeNode(p);
grandparent.add(parent1);
System.out.println ("***p-->"+p) ;
System.out.println ("***p1--->"+p1) ;
}
char b[]=new char[s.length()/2];
char child ;
char ch,ch1;
for(i=0,l=0;i<b.length;i+=2,l++)
{
b[l]=s.charAt(i);
System.out.println ("b[l] "+b[l]);
for(int j=0;j<=i;j++)
{
if(a[i]==b[j])
{
ch1=b[j];
System.out.println("ch1"+ch1);
}
else
{
ch=b[j];
System.out.println("ch"+ch);
}
}
if(p==b[l])
continue;
else
{
child = b[l];
System.out.println (child);
}
//create subtree of a
DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(child);
parent1.add(child11);
}
tree = new JTree(grandparent);
//addtree to a scrollpane
JScrollPane
jsp = new JScrollPane(tree);
add(jsp);//add scrollpane to the contentpane
jlabel = new JLabel();
add(jlabel,BorderLayout.SOUTH);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent tse){
jlabel.setText("Selection is " + tse.getPath());
}
});
}
}
I know this is not a <b>CODEMILL</b>...i have tried this but this only gives me right successor to the depth if i feed this a String.. "fb bc ac"...the complete sequence is..."fb bc ac eb da ga"...currently i am getting result for the "fb bc ac"...sequence of string and that too not very correctly...only getting right hand side of the last child !!!