java中如何展开JTree? 说清楚点。expandrow(int row)是可以的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 < children.length; ++i) add(new FileNode(children[i])); explored = true; } }}class GJApp extends WindowAdapter { static private JPanel statusArea = new JPanel(); static private JLabel status = new JLabel(" "); public static void launch(final JFrame f, String title, final int x, final int y, final int w, int h) { f.setTitle(title); f.setBounds(x,y,w,h); f.setVisible(true); statusArea.setBorder(BorderFactory.createEtchedBorder()); statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0)); statusArea.add(status); status.setHorizontalAlignment(JLabel.LEFT); f.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE); f.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { System.exit(0); } }); } static public JPanel getStatusArea() { return statusArea; } static public void updateStatus(String s) { status.setText(s); }} zzmxw(风) :这是一个文件夹浏览器的例程吗? 谢谢各位,expandrow()可以对树扩展。 老师说这是一个反射可我弄不懂什么意思 Canvas设置背景出错,求解释!!!!! spring调用excel文件,如何调用??有ecxel的API吗? java.lang.NullPointerException 这个循环程序为什么会自己终止? 【新手提问】我点了标题栏上的那个差后,用JOptionPane跳出个有确定和取消的对话框,怎么样能点取消就不退出程序,点确定退出程序呢 请教JAVA调用DLL问题 hibernate中的复各主键问题 问2题JAVA程序 关于流的一个小问题 java中如何运行alter table的语句? 请问session bean中如何获得当前登陆的用户名?
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 < children.length; ++i)
add(new FileNode(children[i])); explored = true;
}
}
}
class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" "); public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true); statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT); f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE); f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void updateStatus(String s) {
status.setText(s);
}
}
这是一个文件夹浏览器的例程吗?