IDEA won't display my JTree

I created a form, bound it to a class and bound all my components to fields in that class. One problem. One of my components is a JTree and when I compile and run all of my components diplay correctly except for the JTree. It still shows color, sports and food, etc. even though I defined all my own tree nodes, etc. This is extremely frustrating. What gives?

8 comments
Comment actions Permalink

On Thu, 01 Apr 2004 00:40:16 +0000, Chris V wrote:

I created a form, bound it to a class and bound all my components to
fields in that class. One problem. One of my components is a JTree and
when I compile and run all of my components diplay correctly except for
the JTree. It still shows color, sports and food, etc. even though I
defined all my own tree nodes, etc. This is extremely frustrating. What
gives?


How are you creating your treenodes and setting the Tree model?

--
Always code as if the guy who ends up maintaining your code will be a
violent psychopath who knows where you live.

Mark Derricutt --- mark@ talios.com --- http://www.talios.com

0
Comment actions Permalink

I've been banging my head against the wall on this for probably about 8 hours now. After doing much research I have found that this is not necessarily an IDEA issue. Anyway, here is an extremely simple tree class that displays sports, food, etc. when it shouldn't.

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class MyTree extends JTree implements TreeSelectionListener {
JTree tree;

public MyTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("rootNode");
DefaultMutableTreeNode child = new DefaultMutableTreeNode("object_SSE");

root.add(child);
DefaultTreeModel tModel = new DefaultTreeModel(root);

tree = new JTree(tModel);

tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(this);
}

public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();

if (node == null) return;

Object nodeInfo = node.getUserObject();
if (node.isLeaf()) {
TreeObject TO = (TreeObject)nodeInfo;
System.out.println(TO.getObject().toString());
}
}

private class TreeObject {
private String object;

public TreeObject(String O) {
object = O;
}

public String getObject() {
return object;
}
}
}

0
Comment actions Permalink

Oh yeah! Very important. I removed the tree component from the IDEA form. I'm putting the tree in a JScrollBar now.

0
Comment actions Permalink

I got my JTree to work.

0
Comment actions Permalink

Any advice for those of us with the exact same problem?

I have a JTree that worked fine when I generated my own GUI code, now that I'm trying to use the IDEA form editor its always coming up as a default tree - Colors, Sports, Food.

I'm guessing the problem is because the constructor to my own class extending JTree is not being called, the base JTree constructor is being called instead. Having to make a 'custom bean descriptor' which is what the documentation implies seems like a huge overkill (if you want to extend a swind class at anytime you must write a descriptor).

Should I just stay away from the UI editor for this small project?

0
Comment actions Permalink

Having to make a 'custom bean descriptor' which is what the documentation implies seems like a huge overkill (if you want to extend a swind class at anytime you must write a descriptor).


+1, especially in a refactoring driven environment.

IntelliJ, why not let the user create the components, e.g. by factories?

Tom

0
Comment actions Permalink

I met the JTree-no-init problem, and i solved it.

if you set GUI designer to compile .form to java source, not directly binary code. you would find all swing objects was constructed and composed into the panel in the background.

so, when you bind the JTree to a 'tree' field, you should know 'tree' will have been constructed into the binding-panel.

this is why write code as:
tree = new JTree(someDefaultTreeModel);
no use. because the panel had been insert another JTree object yet.

if you want to change the tree content. you should write as follows:
tree.setModel(yourTreeModel);

0
Comment actions Permalink

if you want to change the tree content. you should write as follows:
tree.setModel(yourTreeModel);


OK, this is a word-around. If IDEA would allow to layout existing
components, one could create a JTree by passing the model in the
constructor. With the work-around, the first (automatically created)
model will be eaten by the GC.

Tom

0

Please sign in to leave a comment.