当前位置: 技术问答>java相关
jtree的程序查错,愿出150分,请教高手!!
来源: 互联网 发布时间:2015-02-26
本文导语: 程序是显示类似资源管理器的,但是只能显示到倒数第二层,即x目录下还有个y文件,只能显示到x。具体程序太长,请高手们提供mail! | To:gdsean(摇滚java) 确实是看不到,最底一层! To:cuijinjing(...
程序是显示类似资源管理器的,但是只能显示到倒数第二层,即x目录下还有个y文件,只能显示到x。具体程序太长,请高手们提供mail!
|
To:gdsean(摇滚java)
确实是看不到,最底一层!
To:cuijinjing(精灵)
你的代码太长了,不如看看我的。
/*===============================DirBrowserTest.java=========================================*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.JOptionPane;
//------------------------------------------------------------------------------------------
public class DirBrowserTest
{
public static void main(String[] args)
{
JFrame frame = new DirBrowserTestFrame();
frame.show();
}
}
//--------------------------------------------------------------------------------------------
class DirBrowserTestFrame extends JFrame implements ActionListener,TreeSelectionListener
{
public DirBrowserTestFrame()
{
setTitle("DirBrowser");
setSize(400,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
root = new DefaultMutableTreeNode("root");
root.setAllowsChildren(true);
root.add(new DefaultMutableTreeNode(new File("C:\")));
root.add(new DefaultMutableTreeNode(new File("D:\")));
root.add(new DefaultMutableTreeNode(new File("E:\")));
root.add(new DefaultMutableTreeNode(new File("F:\")));
root.add(new DefaultMutableTreeNode(new File("G:\")));
model = new DefaultTreeModel(root);
tree = new JTree(model);
tree.putClientProperty("JTree.lineStyle" , "Angled");
tree.setRootVisible(false);
tree.setEditable(true); //把树设为可编辑
//setup the selection mode
tree.addTreeSelectionListener(this);
int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;
tree.getSelectionModel().setSelectionMode(mode);
model.setAsksAllowsChildren(true); //只有手动允许的节点可以表示为页节点
//this text area holds the current directory
textArea = new JTextArea();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,2));
panel.add(new JScrollPane(tree));
panel.add(new JScrollPane(textArea));
AddDir = new JButton("AddDir");
Delete = new JButton("Delete");
AddDir.addActionListener(this);
Delete.addActionListener(this);
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(AddDir);
p.add(Delete);
Container contentPane = getContentPane();
contentPane.add(panel,"Center");
contentPane.add(p,"South");
}
//--------------------------------------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(selectedNode == null)return;
// DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)selectedNode.getParent();
Object source = event.getSource();
if(source == AddDir)
{
if(!selectedNode.getAllowsChildren())return;
NewDirInfo n = new NewDirInfo("newdir");
if(newDialog == null)newDialog = new NewDirDialog(this);
if(newDialog.showDialog(n))
//String dirname = "";
//if((dirname = JOptionPane.showInputDialog(this , "Please input the new dir name" , "Input Dialog" , JOptionPane.QUESTION_MESSAGE)) != "")
{
try{
String dirname = n.dirname; //由对话框把要新建的文件夹名字取出
System.out.println(dirname); //just for debug!
File parentDir = (File)selectedNode.getUserObject();
File newFile = new File(parentDir + "\" + dirname);
if(!newFile.exists())
{
newFile.mkdir();
textArea.setText("Maked Directory " + dirname);
}
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(newFile);
newNode.setAllowsChildren(true);
System.out.println(selectedNode.getChildCount()); //just for debug!
model.insertNodeInto(newNode,selectedNode,selectedNode.getChildCount());
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Error: " + e);
}
}
return;
}
else if(source == Delete)
{
File targetDir = (File)selectedNode.getUserObject();
if(JOptionPane.showConfirmDialog(this , "Are you sure to delete " + targetDir.toString() , "Delete Confirmed" , JOptionPane.YES_NO_OPTION , JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION)
{
String[] l = targetDir.list();
if(l.length == 0)
{
targetDir.delete();
textArea.setText("Delete Directory " + targetDir);
if(selectedNode.getParent() != null)
model.removeNodeFromParent(selectedNode);
return;
}
else
{
JOptionPane.showMessageDialog(this , "你不能删除一个非空的文件夹!" , "出错" , JOptionPane.ERROR_MESSAGE);
return;
}
}
}
}
//--------------------------------------------------------------------------------------------
public void valueChanged(TreeSelectionEvent event)
{
textArea.setText("");
TreePath path = tree.getSelectionPath();
if(path == null) return;
DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode)path.getLastPathComponent();
if(selectNode.getAllowsChildren())Explorer2(selectNode); //判断是否可以有子节点,才调用Explorer3(),并且显示该目录下所有文件名
else
{
File temp = (File)selectNode.getUserObject();
textArea.setText(temp.toString()); //只显示选中文件的文件名
}
}
//--------------------------------------------------------------------------------------------
/* public void Explorer(DefaultMutableTreeNode p) //检测目录的递归算法,一次性全部Load入,因此速度慢,而且无法显示变化,不可取
{
try{
boolean isAllFiles = true;
String[] list = path.list();
for(int i = 0; i
确实是看不到,最底一层!
To:cuijinjing(精灵)
你的代码太长了,不如看看我的。
/*===============================DirBrowserTest.java=========================================*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.JOptionPane;
//------------------------------------------------------------------------------------------
public class DirBrowserTest
{
public static void main(String[] args)
{
JFrame frame = new DirBrowserTestFrame();
frame.show();
}
}
//--------------------------------------------------------------------------------------------
class DirBrowserTestFrame extends JFrame implements ActionListener,TreeSelectionListener
{
public DirBrowserTestFrame()
{
setTitle("DirBrowser");
setSize(400,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
root = new DefaultMutableTreeNode("root");
root.setAllowsChildren(true);
root.add(new DefaultMutableTreeNode(new File("C:\")));
root.add(new DefaultMutableTreeNode(new File("D:\")));
root.add(new DefaultMutableTreeNode(new File("E:\")));
root.add(new DefaultMutableTreeNode(new File("F:\")));
root.add(new DefaultMutableTreeNode(new File("G:\")));
model = new DefaultTreeModel(root);
tree = new JTree(model);
tree.putClientProperty("JTree.lineStyle" , "Angled");
tree.setRootVisible(false);
tree.setEditable(true); //把树设为可编辑
//setup the selection mode
tree.addTreeSelectionListener(this);
int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;
tree.getSelectionModel().setSelectionMode(mode);
model.setAsksAllowsChildren(true); //只有手动允许的节点可以表示为页节点
//this text area holds the current directory
textArea = new JTextArea();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,2));
panel.add(new JScrollPane(tree));
panel.add(new JScrollPane(textArea));
AddDir = new JButton("AddDir");
Delete = new JButton("Delete");
AddDir.addActionListener(this);
Delete.addActionListener(this);
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(AddDir);
p.add(Delete);
Container contentPane = getContentPane();
contentPane.add(panel,"Center");
contentPane.add(p,"South");
}
//--------------------------------------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(selectedNode == null)return;
// DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)selectedNode.getParent();
Object source = event.getSource();
if(source == AddDir)
{
if(!selectedNode.getAllowsChildren())return;
NewDirInfo n = new NewDirInfo("newdir");
if(newDialog == null)newDialog = new NewDirDialog(this);
if(newDialog.showDialog(n))
//String dirname = "";
//if((dirname = JOptionPane.showInputDialog(this , "Please input the new dir name" , "Input Dialog" , JOptionPane.QUESTION_MESSAGE)) != "")
{
try{
String dirname = n.dirname; //由对话框把要新建的文件夹名字取出
System.out.println(dirname); //just for debug!
File parentDir = (File)selectedNode.getUserObject();
File newFile = new File(parentDir + "\" + dirname);
if(!newFile.exists())
{
newFile.mkdir();
textArea.setText("Maked Directory " + dirname);
}
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(newFile);
newNode.setAllowsChildren(true);
System.out.println(selectedNode.getChildCount()); //just for debug!
model.insertNodeInto(newNode,selectedNode,selectedNode.getChildCount());
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Error: " + e);
}
}
return;
}
else if(source == Delete)
{
File targetDir = (File)selectedNode.getUserObject();
if(JOptionPane.showConfirmDialog(this , "Are you sure to delete " + targetDir.toString() , "Delete Confirmed" , JOptionPane.YES_NO_OPTION , JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION)
{
String[] l = targetDir.list();
if(l.length == 0)
{
targetDir.delete();
textArea.setText("Delete Directory " + targetDir);
if(selectedNode.getParent() != null)
model.removeNodeFromParent(selectedNode);
return;
}
else
{
JOptionPane.showMessageDialog(this , "你不能删除一个非空的文件夹!" , "出错" , JOptionPane.ERROR_MESSAGE);
return;
}
}
}
}
//--------------------------------------------------------------------------------------------
public void valueChanged(TreeSelectionEvent event)
{
textArea.setText("");
TreePath path = tree.getSelectionPath();
if(path == null) return;
DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode)path.getLastPathComponent();
if(selectNode.getAllowsChildren())Explorer2(selectNode); //判断是否可以有子节点,才调用Explorer3(),并且显示该目录下所有文件名
else
{
File temp = (File)selectNode.getUserObject();
textArea.setText(temp.toString()); //只显示选中文件的文件名
}
}
//--------------------------------------------------------------------------------------------
/* public void Explorer(DefaultMutableTreeNode p) //检测目录的递归算法,一次性全部Load入,因此速度慢,而且无法显示变化,不可取
{
try{
boolean isAllFiles = true;
String[] list = path.list();
for(int i = 0; i