The Artima Developer Community
Sponsored Link

Java Answers Forum
JTree Clicking problem

1 reply on 1 page. Most recent reply: Mar 17, 2002 4:10 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Lois

Posts: 4
Nickname: lois
Registered: Mar, 2002

JTree Clicking problem Posted: Mar 17, 2002 4:06 PM
Reply to this message Reply
Advertisement
Hi, I need to implement a JTree that loads its children only upon clicking the node. Everything was fine until when I tried to click the icon to expand and collapse, some runtime error (NullPointerException) appears. Is clicking the + icon and double clicking the folder icon in order to expand/collapse the path not the same? Appended is the code. Thanks....sorry, sorta long. Any comment is welcome.

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.*;
import java.io.*;

public class TreeGUI implements TreeSelectionListener
{
private Controller ct;
private String root;

private ExtendNode rootNode;

private JTree tree;
private DefaultTreeModel tree_model;
private JScrollPane sp;

private ArrayList isExpanded;

public TreeGUI(Controller controller, String rootString) {
//System.out.println("AAAAAAAAAAAAAAAA");
isExpanded = new ArrayList();
ct = controller;
rootNode = buildNode(rootString, rootString);

tree_model = new DefaultTreeModel(rootNode);

tree = new JTree(tree_model);
tree.putClientProperty("JTree.lineStyle", "Angled");
sp = new JScrollPane(tree);
sp.setPreferredSize(new Dimension(500,600));
ct.add(sp);
tree.addTreeSelectionListener(this);
buildTree(rootNode);

}

public void valueChanged(TreeSelectionEvent e) {
System.out.println("\nBBBBBBBBBBBBBBBB");
ExtendNode temp = (ExtendNode)tree.getLastSelectedPathComponent();
//buildTree(temp);
ct.NodeClicked(temp);
//tree.expandPath(new TreePath(temp.getPath()));

}

public String fullPath(ExtendNode node) {
System.out.println("CCCCCCCCCCCCCCCCCCC");
TreeNode[] tn = node.getPath();
String str="";
for (int i=0; i<tn.length; i++)
str += tn.toString() + "\\";
str = str.substring(0, str.length()-1);

if (isExpanded.contains(str)) {
System.out.println("Already contains : " + str);
return null;
}
else {
System.out.println("First time contains : " + str);
isExpanded.add(str);
return str;
}
}

public void buildTree(ExtendNode parentNode) {
System.out.println("11111111111111");

String temp = fullPath(parentNode);

if (temp == null) {
System.out.println("2222222222222");
System.out.println("file is null");
return;
}
else {
System.out.println("3333333333333333");
File file = new File(temp);
if (file.isDirectory()) {
System.out.println("44444444444444444");
String[] childFiles = file.list();
Arrays.sort(childFiles);
for (int i=0; i<childFiles.length; i++) {
System.out.println("55555555555555555");
System.out.println("ChildFiles : " + childFiles.toString());
ExtendNode childNode = buildNode(childFiles, temp + '\\' + childFiles);
tree_model.insertNodeInto(childNode, parentNode,0);
}
}
else {
System.out.println("666666666666");
return;
}
}
}

public ExtendNode buildNode(String node, String absPath) {
System.out.println("EEEEEEEEEEEEEEEEEEE");
System.out.println("Node : " + node);
File file = new File(absPath);

if (file.isDirectory()) {
return new ExtendNode(node, false);
}
else
return new ExtendNode(node, true);
}
}

import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;

public class ExtendNode extends DefaultMutableTreeNode {

private boolean leaf;

public ExtendNode(String arg, boolean leaf) {
super(arg);
this.leaf = leaf;
}

public boolean isLeaf() {
return leaf;
}


}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: JTree Clicking problem Posted: Mar 17, 2002 4:10 PM
Reply to this message Reply
Quick comment, before I've looked very closely: Please use the java formatting tags mentioned in "Formatting Your Post" at the bottom of the page!

Flat View: This topic has 1 reply on 1 page
Topic: Opening Large files(3MB) in a text editor. Previous Topic   Next Topic Topic: Java Telephony

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use