当前位置: 技术问答>java相关
各位,菜鸟问如何建立目录树,开口有分了。
来源: 互联网 发布时间:2015-02-17
本文导语: | import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import com.borland.jbcl.layout.*; import javax.swing.*; import javax.swing.tree.*; import javax.swing.event.*; public class ChooseDir extends JDialog { public static final ImageIc...
|
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class ChooseDir extends JDialog
{
public static final ImageIcon ICON_COMPUTER =
new ImageIcon(FrameMain.class.getResource("images/paste.gif"));
public static final ImageIcon ICON_DISK =
new ImageIcon(FrameMain.class.getResource("images/new.gif"));
public static final ImageIcon ICON_FOLDER =
new ImageIcon(FrameMain.class.getResource("images/folder.gif"));
public static final ImageIcon ICON_EXPANDEDFOLDER =
new ImageIcon(FrameMain.class.getResource("images/expandedfolder.gif"));
JButton jButtonOK = new JButton("OK");
JButton jButtonCancel = new JButton("Cancel");
protected JTree m_tree;
protected DefaultTreeModel m_model;
protected JTextField m_display;
JTextField m_textPath;
public ChooseDir(JTextField jTextpath)
{
//super("Directories Tree");
this.setModal(true);
setSize(400, 300);
m_textPath = jTextpath;
DefaultMutableTreeNode top = new DefaultMutableTreeNode(
new IconData(ICON_COMPUTER, null, "Computer"));
jButtonOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonOK_actionPerformed(e);
}
});
jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonCancel_actionPerformed(e);
}
});
DefaultMutableTreeNode node;
File[] roots = File.listRoots();
for (int k=0; k 0 ? m_file.getName() :
m_file.getPath();
}
public boolean expand(DefaultMutableTreeNode parent)
{
DefaultMutableTreeNode flag =
(DefaultMutableTreeNode)parent.getFirstChild();
if (flag==null) // No flag
return false;
Object obj = flag.getUserObject();
if (!(obj instanceof Boolean))
return false; // Already expanded
parent.removeAllChildren(); // Remove Flag
File[] files = listFiles();
if (files == null)
return true;
Vector v = new Vector();
for (int k = 0; k
import java.awt.event.*;
import java.io.*;
import java.util.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class ChooseDir extends JDialog
{
public static final ImageIcon ICON_COMPUTER =
new ImageIcon(FrameMain.class.getResource("images/paste.gif"));
public static final ImageIcon ICON_DISK =
new ImageIcon(FrameMain.class.getResource("images/new.gif"));
public static final ImageIcon ICON_FOLDER =
new ImageIcon(FrameMain.class.getResource("images/folder.gif"));
public static final ImageIcon ICON_EXPANDEDFOLDER =
new ImageIcon(FrameMain.class.getResource("images/expandedfolder.gif"));
JButton jButtonOK = new JButton("OK");
JButton jButtonCancel = new JButton("Cancel");
protected JTree m_tree;
protected DefaultTreeModel m_model;
protected JTextField m_display;
JTextField m_textPath;
public ChooseDir(JTextField jTextpath)
{
//super("Directories Tree");
this.setModal(true);
setSize(400, 300);
m_textPath = jTextpath;
DefaultMutableTreeNode top = new DefaultMutableTreeNode(
new IconData(ICON_COMPUTER, null, "Computer"));
jButtonOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonOK_actionPerformed(e);
}
});
jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonCancel_actionPerformed(e);
}
});
DefaultMutableTreeNode node;
File[] roots = File.listRoots();
for (int k=0; k 0 ? m_file.getName() :
m_file.getPath();
}
public boolean expand(DefaultMutableTreeNode parent)
{
DefaultMutableTreeNode flag =
(DefaultMutableTreeNode)parent.getFirstChild();
if (flag==null) // No flag
return false;
Object obj = flag.getUserObject();
if (!(obj instanceof Boolean))
return false; // Already expanded
parent.removeAllChildren(); // Remove Flag
File[] files = listFiles();
if (files == null)
return true;
Vector v = new Vector();
for (int k = 0; k