Welcome to Dream.In.Code
Become a Java Expert!

Join 150,381 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,190 people online right now. Registration is fast and FREE... Join Now!




JTree and Recursive

 
Reply to this topicStart new topic

JTree and Recursive

fsloke
24 Jan, 2008 - 09:30 PM
Post #1

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 258



Thanked: 4 times
My Contributions
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
User is offlineProfile CardPM
+Quote Post

Jayman
RE: JTree And Recursive
24 Jan, 2008 - 11:32 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Use code.gif tags when posting your code.

Describe the problem you are having in as much detail as possible.
User is offlineProfile CardPM
+Quote Post

fsloke
RE: JTree And Recursive
25 Jan, 2008 - 01:10 AM
Post #3

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 258



Thanked: 4 times
My Contributions
Plan to do recursive function one the tree node...

But not success yet...


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:20PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month