当前位置: 技术问答>java相关
怎么在JTree中显示userObject?
来源: 互联网 发布时间:2015-01-31
本文导语: 我为一个userobject创建了jtree,需要在节点中显示userobject的一个属性 public class userobject{ String name; boolean isHidden; public String toString(){ return name; } } 请问如何在jtree中的节点显示为userobject的name?用usero...
我为一个userobject创建了jtree,需要在节点中显示userobject的一个属性
public class userobject{
String name;
boolean isHidden;
public String toString(){
return name;
}
}
请问如何在jtree中的节点显示为userobject的name?用userobject的toString 怎么不管用?
public class userobject{
String name;
boolean isHidden;
public String toString(){
return name;
}
}
请问如何在jtree中的节点显示为userobject的name?用userobject的toString 怎么不管用?
|
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.EventObject;
public class Test extends JFrame {
public Test() {
final JTree tree = new JTree(createTreeModel());
JScrollPane scrollPane = new JScrollPane(tree);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(GJApp.getStatusArea(),
BorderLayout.SOUTH);
tree.addTreeExpansionListener(new TreeExpansionListener(){
public void treeCollapsed(TreeExpansionEvent e) {
}
public void treeExpanded(TreeExpansionEvent e) {
UpdateStatus updateThread;
TreePath path = e.getPath();
FileNode node = (FileNode)
path.getLastPathComponent();
if( ! node.isExplored()) {
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
GJApp.updateStatus("exploring ...");
UpdateStatus us = new UpdateStatus();
us.start();
node.explore();
model.nodeStructureChanged(node);
}
}
class UpdateStatus extends Thread {
public void run() {
try { Thread.currentThread().sleep(450); }
catch(InterruptedException e) { }
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GJApp.updateStatus(" ");
}
});
}
}
});
}
private DefaultTreeModel createTreeModel() {
File root = new File("E:/");
FileNode rootNode = new FileNode(root);
rootNode.explore();
return new DefaultTreeModel(rootNode);
}
public static void main(String args[]) {
GJApp.launch(new Test(),"JTree File Explorer",
300,300,450,400);
}
}
class FileNode extends DefaultMutableTreeNode {
private boolean explored = false;
public FileNode(File file) {
setUserObject(file);
}
public boolean getAllowsChildren() { return isDirectory(); }
public boolean isLeaf() { return !isDirectory(); }
public File getFile() { return (File)getUserObject(); }
public boolean isExplored() { return explored; }
public boolean isDirectory() {
File file = getFile();
return file.isDirectory();
}
public String toString() {
File file = (File)getUserObject();
String filename = file.toString();
int index = filename.lastIndexOf(File.separator);
return (index != -1 && index != filename.length()-1) ?
filename.substring(index+1) :
filename;
}
public void explore() {
if(!isDirectory())
return;
if(!isExplored()) {
File file = getFile();
File[] children = file.listFiles();
for(int i=0; i
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.EventObject;
public class Test extends JFrame {
public Test() {
final JTree tree = new JTree(createTreeModel());
JScrollPane scrollPane = new JScrollPane(tree);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(GJApp.getStatusArea(),
BorderLayout.SOUTH);
tree.addTreeExpansionListener(new TreeExpansionListener(){
public void treeCollapsed(TreeExpansionEvent e) {
}
public void treeExpanded(TreeExpansionEvent e) {
UpdateStatus updateThread;
TreePath path = e.getPath();
FileNode node = (FileNode)
path.getLastPathComponent();
if( ! node.isExplored()) {
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
GJApp.updateStatus("exploring ...");
UpdateStatus us = new UpdateStatus();
us.start();
node.explore();
model.nodeStructureChanged(node);
}
}
class UpdateStatus extends Thread {
public void run() {
try { Thread.currentThread().sleep(450); }
catch(InterruptedException e) { }
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GJApp.updateStatus(" ");
}
});
}
}
});
}
private DefaultTreeModel createTreeModel() {
File root = new File("E:/");
FileNode rootNode = new FileNode(root);
rootNode.explore();
return new DefaultTreeModel(rootNode);
}
public static void main(String args[]) {
GJApp.launch(new Test(),"JTree File Explorer",
300,300,450,400);
}
}
class FileNode extends DefaultMutableTreeNode {
private boolean explored = false;
public FileNode(File file) {
setUserObject(file);
}
public boolean getAllowsChildren() { return isDirectory(); }
public boolean isLeaf() { return !isDirectory(); }
public File getFile() { return (File)getUserObject(); }
public boolean isExplored() { return explored; }
public boolean isDirectory() {
File file = getFile();
return file.isDirectory();
}
public String toString() {
File file = (File)getUserObject();
String filename = file.toString();
int index = filename.lastIndexOf(File.separator);
return (index != -1 && index != filename.length()-1) ?
filename.substring(index+1) :
filename;
}
public void explore() {
if(!isDirectory())
return;
if(!isExplored()) {
File file = getFile();
File[] children = file.listFiles();
for(int i=0; i