Results 1 to 8 of 8
  1. #1
    deemu is offline Member
    Join Date
    Feb 2012
    Posts
    15
    Rep Power
    0

    Default How to create a tree structure

    Can anyone help me to create a tree structure in applet in real time java ?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: how to create a tree structure in applet in real time java ?

    Can you be more specific? Are you drawing or creating nodes?
    What does being in an applet have to do with this project?

  3. #3
    deemu is offline Member
    Join Date
    Feb 2012
    Posts
    15
    Rep Power
    0

    Default Re: how to create a tree structure in applet in real time java ?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.JTree;
    import javax.swing.ScrollPaneConstants;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreePath;
    
    public class MainClass {
    
      public static void main(String[] a) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JTreeEvents());
        f.setSize(500, 500);
        f.setVisible(true);
      }
    }
    
    class JTreeEvents extends JPanel {
      JTree tree;
    
      JTextField jtf;
    
      public JTreeEvents() {
        setLayout(new BorderLayout());
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
    
        DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
        top.add(a);
        
        a.add(new DefaultMutableTreeNode("A1"));
        a.add(new DefaultMutableTreeNode("A2"));
    
        DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
        top.add(b);
        
        b.add(new DefaultMutableTreeNode("B1"));
        b.add(new DefaultMutableTreeNode("B2"));
        b.add(new DefaultMutableTreeNode("B3"));
    
        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);
    
        add(jsp, BorderLayout.CENTER);
    
        jtf = new JTextField("", 20);
        add(jtf, 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)
          jtf.setText(tp.toString());
        else
          jtf.setText("");
      }
    }
    *****************

    In this above coding, we gave the input and get the same output.

    but in my case ,what i have given in my website that must be in tree form like this.
    Last edited by Norm; 03-07-2012 at 04:31 PM. Reason: added code tags; moved to new java

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to create a tree structure

    What do I do when I execute this program to see the problem?

  5. #5
    deemu is offline Member
    Join Date
    Feb 2012
    Posts
    15
    Rep Power
    0

    Default Re: How to create a tree structure

    Sorry i cant get you

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to create a tree structure

    The program you posted compiles and executes. What is wrong with it?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to create a tree structure

    Quote Originally Posted by deemu View Post
    Can anyone help me to create a tree structure in applet in real time java ?
    In real time? Do you know that a tree has a growth rate of less than 15" per year? Have a look at the Thread.sleep( ... ) method; you'll need it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    deemu is offline Member
    Join Date
    Feb 2012
    Posts
    15
    Rep Power
    0

Similar Threads

  1. Tree data structure
    By Nacao in forum New To Java
    Replies: 18
    Last Post: 08-23-2011, 07:26 PM
  2. Tree Structure
    By chiku in forum New To Java
    Replies: 2
    Last Post: 01-27-2011, 09:31 PM
  3. Problem--How to create dynamic tree structure in jsp using javascript
    By natu in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-11-2010, 09:11 AM
  4. standart tree structure?
    By willemien in forum New To Java
    Replies: 3
    Last Post: 05-06-2010, 08:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •