Ok. I loaded this code. It runs. What's the problem?
And - more to the pint - what IS "the expected result"?
Main Topics
Browse All Topicsi tried to create a package explorer, but it doesn't work well...can anyone say me what is the problem
import java.awt.BorderLayout;
import java.util.Vector;
import java.awt.Container;
import java.awt.HeadlessException
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
class MyWindow extends JFrame {
protected Container c;
public MyWindow() throws HeadlessException {
this("Window1");
}
public MyWindow(String arg0) throws HeadlessException {
super(arg0);
this.setDefaultCloseOperat
this.setSize(300,400);
c = this.getContentPane();
}
}
public class Test3 extends MyWindow {
JTree tree;
public Test3() {
super("Creating a Simple JTree");
String[] hierarchy = {"javax.swing", "javax.swing.border",
"javax.swing.colorchooser"
"javax.swing.filechooser",
"javax.swing.plaf.basic", "javax.swing.plaf.metal",
"javax.swing.plaf.multi", "javax.swing.table",
"javax.swing.text", "javax.swing.text.html",
"javax.swing.text.html.par
"javax.swing.tree", "javax.swing.undo"};
Vector root = new Vector();
for (int i = 0; i < hierarchy.length; i++) {
getList(hierarchy[i].split
}
System.out.println(root);
tree = new JTree(root);
c.add(new JScrollPane(tree), BorderLayout.CENTER);
setVisible(true);
}
public void getList(String[] x,Vector root) {
for (int i = 0; i < x.length; i++) {
root = vx(root,x[i]);
}
}
public Vector vx(Vector x, String y) {
Vector v1 = new Vector();
v1.add(y);
x.add(v1);
return v1;
}
public static void main(String[] args) {
new Test3();
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ok, I see what you are missing - you are not actually constructing the tree. try this
public class Test4
extends MyWindow {
JTree tree;
Map map = new HashMap();
public Test4() {
super("Creating a Simple JTree");
String[] hierarchy = {
"javax.swing", "javax.swing.border", "javax.swing.colorchooser"
"javax.swing.plaf", "javax.swing.plaf.basic", "javax.swing.plaf.metal", "javax.swing.plaf.multi",
"javax.swing.table", "javax.swing.text", "javax.swing.text.html", "javax.swing.text.html.par
"javax.swing.text.rtf", "javax.swing.tree", "javax.swing.undo"};
DefaultMutableTreeNode root = new DefaultMutableTreeNode("pa
for (int i = 0; i < hierarchy.length; i++) {
getList(hierarchy[i], root);
}
tree = new JTree(root);
c.add(new JScrollPane(tree), BorderLayout.CENTER);
setVisible(true);
}
public void getList(String s, DefaultMutableTreeNode root) {
String[] x = s.split("\\.");
DefaultMutableTreeNode parent = null;
String fullname = ".";
for (int i = 0; i < x.length; i++) {
if (i == 0) {
parent = root;
fullname = x[0];
} else {
parent = (DefaultMutableTreeNode) map.get(fullname);
fullname += "." + x[i];
}
DefaultMutableTreeNode node = new DefaultMutableTreeNode(x[i
if (parent != null) {
if (!map.containsKey(fullname
parent.add(node);
map.put(fullname, node);
}
}
}
}
public static void main(String[] args) {
new Test4();
}
}
Of course - but who is writing this program? The map exists only while you are building the tree - it is then garbage collected. I do not beleive you are so short of memory.
Basically the problem is that you need to find the entry to attach to - the parent. You could keep the previouos parent and see if it's the right one (and then search its parent, etc - pretty painful), or search the entire tree looking for the parent. But this is swapping computational complexity for memory - a poor swap (since I started programming, computers have become a million times faster. As I have not, I tend towards solutions that make the computer work harder, rather than me).
The Map is a decent solution and using memory but fleetingly.
If you want to be sure it is indeed garbage collected:
public class Test4
extends MyWindow {
JTree tree;
public Test4() {
super("Creating a Simple JTree");
String[] hierarchy = {
"javax.swing", "javax.swing.border", "javax.swing.colorchooser"
"javax.swing.plaf", "javax.swing.plaf.basic", "javax.swing.plaf.metal", "javax.swing.plaf.multi",
"javax.swing.table", "javax.swing.text", "javax.swing.text.html", "javax.swing.text.html.par
"javax.swing.text.rtf", "javax.swing.tree", "javax.swing.undo"};
DefaultMutableTreeNode root = new DefaultMutableTreeNode("pa
Map map = new HashMap();
for (int i = 0; i < hierarchy.length; i++) {
getList(hierarchy[i], root, map);
}
tree = new JTree(root);
c.add(new JScrollPane(tree), BorderLayout.CENTER);
setVisible(true);
}
public void getList(String s, DefaultMutableTreeNode root, Map map) {
String[] x = s.split("\\.");
DefaultMutableTreeNode parent = null;
String fullname = ".";
for (int i = 0; i < x.length; i++) {
if (i == 0) {
parent = root;
fullname = x[0];
} else {
parent = (DefaultMutableTreeNode) map.get(fullname);
fullname += "." + x[i];
}
DefaultMutableTreeNode node = new DefaultMutableTreeNode(x[i
if (parent != null) {
if (!map.containsKey(fullname
parent.add(node);
map.put(fullname, node);
}
}
}
}
public static void main(String[] args) {
new Test4();
}
}
Business Accounts
Answer for Membership
by: maheshexpPosted on 2004-11-20 at 12:03:03ID: 12635334
also i tried with this code, but i can't ge expected result
, "javax.swing.event", "javax.swing.plaf", ser", "javax.swing.text.rtf",
ckages",tr ue); ; ;
]);
public class Test3 extends MyWindow {
JTree tree;
public Test3() {
super("Creating a Simple JTree");
String[] hierarchy = {"javax.swing", "javax.swing.border",
"javax.swing.colorchooser"
"javax.swing.filechooser",
"javax.swing.plaf.basic", "javax.swing.plaf.metal",
"javax.swing.plaf.multi", "javax.swing.table",
"javax.swing.text", "javax.swing.text.html",
"javax.swing.text.html.par
"javax.swing.tree", "javax.swing.undo"};
DefaultMutableTreeNode root = new DefaultMutableTreeNode("pa
getList(hierarchy[1],root)
getList(hierarchy[2],root)
tree = new JTree(root);
c.add(new JScrollPane(tree), BorderLayout.CENTER);
setVisible(true);
}
public void getList(String s, DefaultMutableTreeNode root) {
String[]x = s.split("\\.");
for (int i = 0; i < x.length; i++) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(x[i
root.add(node);
root = node;
}
}
public static void main(String[] args) {
new Test3();
}
}