I just wan to create a Tree... With the data I have dynamically....
I have problem with my recursive problem
<<-----------------------------code --->>>>>
CODE
import javax.swing.*;
import java.awt.event.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.JOptionPane.*;
public class Lab extends JFrame implements ActionListener
{
private JTree tree1;
private JLabel showName;
private JButton updateBtn;
public static void main(String [] args)
{
Lab a=new Lab();
}
public Lab()
{
this.setSize(700,600);
this.setTitle("Java JTree");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
tree1 = createTree();
tree1.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION);
tree1.setVisibleRowCount(16);
tree1.addTreeSelectionListener(new TreeListener());
JScrollPane scroll = new JScrollPane(tree1);
panel1.add(scroll);
showName = new JLabel();
panel1.add(showName);
updateBtn = new JButton("Update");
updateBtn.addActionListener(this);
panel1.add(updateBtn);
this.add(panel1);
this.setVisible(true);
}
public JTree createTree()
{
//Array store
// odd number is the node name
// Example tree
// Java is the Best --> no represent
// + Level1 --> 1
// -file1 --> 10
// -file2 --> 10
// +Level12 --> 11
// -file3 --> 110
// + Level2 --> 2
// -file3 --> 20
// '0' at behind mean file otherwise is the file
ArrayList<String> ayat =new ArrayList();
ayat.add("Level1");
ayat.add("1");
ayat.add("file1");
ayat.add("10");
ayat.add("file2");
ayat.add("10");
ayat.add("Level12");
ayat.add("11");
ayat.add("file3");
ayat.add("110");
ayat.add("Level2");
ayat.add("2");
ayat.add("20");
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Java is the Best");
createNode( ayat, top, 0);
return new JTree(top);
}
//The problem is here
// You all can see the logic behind
// Child will created then point to it parent... point finish all "1" first then back to his on parent....
public int createNode(ArrayList<String> ayat, DefaultMutableTreeNode top, int i)
{
DefaultMutableTreeNode category=top;
DefaultMutableTreeNode book = null;
int j=i;
System.out.println("Start New Function:j="+j);
TreeNode node=top.getLastLeaf();
String categoryName= node.toString();
while(i!=ayat.size()-1)
{
System.out.println("Check: j="+j +" i="+i);
System.out.println("Category:"+categoryName);
System.out.println("Ayat 0:"+ ayat.get(i));
System.out.println("Ayat 1:"+ ayat.get(i+1));
if(ayat.get(i+1).length()==1)
{
System.out.println("Selection 1");
System.out.println("j=" + j + " "+"i="+i);
System.out.println("Created Node for "+ayat.get(i) +" --> top");
categoryName =ayat.get(i)+"";
category = new DefaultMutableTreeNode(ayat.get(i));
top.add(category);
i=i+2;
}
else if(ayat.get(i+1).substring(0,ayat.get(i+1).length()-1).compareTo(ayat.get(j+1))==0 && (j<=i))
{
System.out.println("Selection 2");
System.out.println("Condition 1: Curent front compare to previous");
System.out.println(ayat.get(i+1).substring(0,ayat.get(i+1).length()-1) + "<-->"+ayat.get(i-1)+":"+ (ayat.get(i+1).substring(0,ayat.get(i+1).length()-1).compareTo(ayat.get(i-1))==0));
System.out.println("Condition 2: j<i");
System.out.println("Answer:" +(j<=i));
System.out.println("Created Node for "+ayat.get(i) +" --> "+ category);
book = new DefaultMutableTreeNode(ayat.get(i));
category.add(book);
i=createNode(ayat,book,i+2);
}
else
{
System.out.println("Selection 3");
System.out.println("Break");
break;
}
}
System.out.println("Start end Function:j="+j);
return i;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==updateBtn)
{
JOptionPane.showMessageDialog(null,"Play","Play",1);
}
}
public void updateTree(){
tree1 = createTree();
tree1.repaint();
}
private class TreeListener implements TreeSelectionListener
{
public void valueChanged(TreeSelectionEvent e)
{
Object o = tree1.getLastSelectedPathComponent();
DefaultMutableTreeNode show;
show = (DefaultMutableTreeNode) o;
//show = (TreeNode)o;
TreeNode [] b = show.getPath();//getUserObject();
String title="";
for(int i=0;i<b.length;i++)
title=title+"/"+b[i];
showName.setText(title);
}
}
}
<<---code---->>>
Thank you...
-fsloke