Solved

Changing the tree in JTree?

Posted on 2001-06-12
6
343 Views
Last Modified: 2010-08-05
I'm using Forte IDE and added a JTree via drag and drop.

It creates a default tree so now I want to change it.
the problem is that all the samples I have uses the following code:

DefaultMutableTreeNode root = new DefaultMutableTreeNode( "Top" );

DefaultMutableTreeNode col21 = new DefaultMutableTreeNode ("Line1" );

root.add(col21);

DefaultMutableTreeNode col22 = new DefaultMutableTreeNode ("line2" );

col21.add(col22);

JTree tree = new JTree( root );

The problem is that Forte creates the instance of JTree already and its readonly since this is the part that the ide uses.  So I cant create the tree when creating a new instance of the tree.

Is there another way of doing this?  Maybe a different method?

Thanks
0
Comment
Question by:eric07
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:david_d
ID: 6184947
Hi,

In the declarations you want to write:

  DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
  TreePath tp = null;
  JTree jTree1 = new JTree(root);


This creates a root node and assigns it to the JTree (like you have done).

Below is a method which is called when an add button is pressed by the mouse.  It takes a string from a JTextField (addTextField) and adds a def. mut. tree node to the selected node - from the tree.

  void addButton_mouseClicked(MouseEvent e)
  {
    if(addTextField.getText().length() > 0)
    {
      try
      {
        //create def. mut. tree node
          //and add text to it
          DefaultMutableTreeNode dMTN = new DefaultMutableTreeNode(addTextField.getText());
        //create a treepath and assign the selected path
          //from the JTree (the path that is currently selected)
          TreePath myTreePath = jTree1.getSelectionPath();

          //Obtain the last def. mut. tree node from the tree
          //and assign it to Temp
        DefaultMutableTreeNode temp = (DefaultMutableTreeNode)myTreePath.getLastPathComponent();
        //Add the new Def. mut. tree node to temp
          temp.add(dMTN);
          //Update the tree
        jTree1.updateUI();
          //Clear the text field.
        addTextField.setText(new String());
      }
      catch(Exception ee)
      {
        JOptionPane.showMessageDialog(this, "No Node is selected.\nPlease select then press the 'Add' button.");
      }

    }
    else
    {
      JOptionPane.showMessageDialog(this, "No data entered!\nPlease try again!");
    }

  }

This code is taken from a small Swing applet that I have created, if you want the code, html file and everything, provide your e-mail address!

I hope this helps.

David
0
 

Author Comment

by:eric07
ID: 6187236
What about this problem though.

Since Forte already did a new on the JTree control.
and now your code wants me to do a another new.  Isnt that creating another instance of the control?  And if so is that ok?

JTree tree = new JTree( root );

The problem is that Forte creates the instance of JTree already and its readonly since this is the part
that the ide uses.  So I cant create the tree when creating a new instance of the tree.
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6189082
Hi,

Create you data as DefaultTreeModel and set it using JTree.setModel().
Or take existing model using getModel() (It will amost certainly be
DefaultTreeModel with DefaultMutableTreeNodes inside) and modify it.

Regards,
Igor Bazarny
Brainbench MVP for Java 1
www.brainbench.com
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:eric07
ID: 6189230
Do you happen to have a sample on how to do this.  Excuse my ignorance since i'm new to this.

Thanks
0
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 50 total points
ID: 6193407
OK, can you find a place in code where you want to update model? I'm not familiar with code generated by Forte. There should be init method or something like. At that place you can create your model:

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
    root.add(new DefaultMutableTreeNode("child1"));
    root.add(new DefaultMutableTreeNode("child2"));

    DefaultTreeModel model = new DefaultTreeModel(root);

And show that model in JTree:

    tree.setModel(model);

Regards,
Igor Bazarny
0
 

Author Comment

by:eric07
ID: 6193916
Thanks allot bazarny you really helped me.  I totally missed setModel() member function.  That did the trick
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
create a gui in perl 3 69
micro services vs rest web services 16 84
Java passing arguments (type error) 2 48
JList custom Cell Renderer refresh 15 53
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now