关于JTree组件?
一直采用javascript来实现树型目录,然而它所带来的速度问题可能会导致系统死机,我想采用JTree来重新实现树型目录,不知道谁有没有现成的实例可以用,万分感谢!
li.a.jin@163.com
问题点数:0、回复次数:3Top
1 楼Keepers(中文昵称)回复于 2003-09-01 13:15:28 得分 0
/*
* Created on 2003/06/16
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.tide.test.rmi;
/**
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.io.*;
import java.rmi.*;
import java.util.Enumeration;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.tree.*;
public class FileClient extends JFrame {
JPanel contentPane;
DefaultMutableTreeNode root = new DefaultMutableTreeNode("WANG");
BorderLayout borderLayout1 = new BorderLayout();
JSplitPane jSplitPane1 = new JSplitPane();
JSplitPane jSplitPane2 = new JSplitPane();
JFileChooser jFileChooser1 = new JFileChooser();
JScrollPane jScrollPane1 = new JScrollPane();
JTree jTree1 = new JTree(root);
JToolBar jToolBar1 = new JToolBar();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
// private String serverName = "//" + argv[1] + "/FileServer";
//Construct the frame
public FileClient() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
root.insert(new DefaultMutableTreeNode("C:"), 0);
root.insert(new DefaultMutableTreeNode("D:"), 1);
root.insert(new DefaultMutableTreeNode("E:"), 2);
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(595, 366));
this.setTitle("RMI Frame");
jSplitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
jButton2.setText("GetFileList");
jButton2.addMouseListener(new MainFrame_jButton2_mouseAdapter(this));
jButton3.setText("GetFile");
jButton4.setText("Close");
contentPane.add(jSplitPane1, BorderLayout.CENTER);
jSplitPane1.add(jSplitPane2, JSplitPane.LEFT);
jSplitPane2.add(jScrollPane1, JSplitPane.TOP);
jSplitPane2.add(jToolBar1, JSplitPane.BOTTOM);
jToolBar1.add(jButton2, null);
jToolBar1.add(jButton3, null);
jToolBar1.add(jButton4, null);
jScrollPane1.getViewport().add(jTree1, null);
jSplitPane1.add(jFileChooser1, JSplitPane.RIGHT);
jSplitPane1.setDividerLocation(250);
jSplitPane2.setDividerLocation(320);
this.jFileChooser1.showSaveDialog(this);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public void getFile(String diskName) {
try {
int i = 0;
for (i = 0; i < root.getChildCount(); i++) {
DefaultMutableTreeNode model =
(DefaultMutableTreeNode) root.getChildAt(i);
if (model.toString().equalsIgnoreCase(diskName)) {
break;
}
}
FileDownloader fi =
(FileDownloader) Naming.lookup("//" + "wang" + "/FileServer");
File[] files = fi.getFileList(diskName + "\\");
System.out.println(files.length);
for (int j = 0; j < files.length; j++) {
MutableTreeNode newNode =
new DefaultMutableTreeNode(files[j].getName());
// Insert new node as last child of node
((DefaultMutableTreeNode) root.getChildAt(i)).add(newNode);
}
jTree1 = new JTree(root);
expandAll(jTree1, true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
void jButton2_mouseReleased(MouseEvent e) {
getFile(jTree1.getSelectionPath().getLastPathComponent().toString());
}
public static void main(String[] args) {
FileClient client = new FileClient();
client.show();
}
public void expandAll(JTree tree, boolean expand) {
TreeNode root = (TreeNode) tree.getModel().getRoot();
// Traverse tree from root
expandAll(tree, new TreePath(root), expand);
}
private void expandAll(JTree tree, TreePath parent, boolean expand) {
// Traverse children
TreeNode node = (TreeNode) parent.getLastPathComponent();
if (node.getChildCount() >= 0) {
for (Enumeration e = node.children(); e.hasMoreElements();) {
TreeNode n = (TreeNode) e.nextElement();
TreePath path = parent.pathByAddingChild(n);
expandAll(tree, path, expand);
}
}
// Expansion or collapse must be done bottom-up
if (expand) {
tree.expandPath(parent);
} else {
tree.collapsePath(parent);
}
}
}
class MainFrame_jButton2_mouseAdapter extends java.awt.event.MouseAdapter {
FileClient adaptee;
MainFrame_jButton2_mouseAdapter(FileClient adaptee) {
this.adaptee = adaptee;
}
public void mouseReleased(MouseEvent e) {
adaptee.jButton2_mouseReleased(e);
}
}Top
2 楼Keepers(中文昵称)回复于 2003-09-01 13:21:04 得分 0
我做的是从RMI取得服务器端的目录结构,然后放入一个树中,希望对你有帮助Top
3 楼liajin(Alde Lee)回复于 2003-09-01 14:08:19 得分 0
谢谢
我先研究研究Top




