问题描述
在我的项目中,我正在尝试添加文件资源管理器,以便用户可以从给定目录中选择文件。我想将此视图限制为项目的根文件夹(由用户确定)。这与Eclipses Package Explorer非常相似,因为工作区由用户决定。
目前文件不显示完整路径(来自C :),这是我想要什么,但所有文件夹都显示完整路径(我不想要,我只想要文件夹名称)。
那么JTree如何显示这些名称?
我已经看到JTree使用File.tostring()方法,但是当我实现自己的文件并覆盖toString方法时,没有任何改变。
这是我的代码:
import java.awt.BorderLayout;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.util.Collections;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
公共类pnl_fileView扩展JPanel {
/ **
*在JTree视图中显示文件系统
*
* @version $ Id:FileTree .java,v 1.9 2004/02/23 03:39:22 ian Exp $
* @author Ian Darwin
* /
/ **构建一个FileTree * /
// public pnl_fileView(){
private myFile projectFile;
public pnl_fileView(myFile dir){
//创建文件资源管理器
//需要添加根文件夹更改的设置。
//Config.getProject(); //这将从配置文件中获取当前文件。
//开始选择File
if(projectFile == null){
JFileChooser chooser;
String choosertitle =请选择一个根文件夹;
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File(。));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//
//禁用所有文件选项。
//
chooser.setAcceptAllFileFilterUsed(false);
//
if(chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION){
System.out.println(getCurrentDirectory():+ chooser.getCurrentDirectory());
System.out.println(getSelectedFile():+ chooser.getSelectedFile());
projectFile = new myFile(chooser.getSelectedFile()。getAbsolutePath());
// projectFile = chooser.getSelectedFile();
}
else {
System.out.println(No Selection);
}
}
else {
//找出文件系统中开始显示
的位置}
//结束选择文件
setLayout(new BorderLayout());
//创建一个包含所有节点的树列表,并使其成为JTree
JTree树=新JTree(addNodes(null,projectFile));
//添加一个监听器
tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent e){
DefaultMutableTreeNode node =(DefaultMutableTreeNode)e
.getPath()。getLastPathComponent();
System.out.println(你选择+节点);
}
});
//最后,将JTree放入JScrollPane。
JScrollPane scrollpane = new JScrollPane();
scrollpane.getViewport()。add(tree);
add(BorderLayout.CENTER,scrollpane);
}
/ **将dir下的节点添加到curTop中。高度递归。 * /
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop,myFile dir){
String curPath = dir.getPath();
System.out.println(curPath);
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if(curTop!= null){//应该只在null
curTop.add(curDir)时为null;
}
Vector< String> ol = new Vector< String>();
String [] tmp = dir.list();
for(int i = 0; i< tmp.length; i ++)
ol.addElement(tmp [i]);
Collections.sort(ol,String.CASE_INSENSITIVE_ORDER);
myFile f;
Vector< String> files = new Vector< String>();
//两个传球,一个用于Dirs,一个用于文件。这是#1。
for(int i = 0; i< ol.size(); i ++){
String thisObject =(String)ol.elementAt(i);
String newPath;
if(curPath.equals(。))
newPath = thisObject;
else
newPath = curPath + myFile.separator + thisObject;
System.out.println(这是路径:+ newPath);
if((f = new myFile(newPath))。isDirectory())
addNodes(curDir,f);
else
files.addElement(thisObject);
}
//传递两个:用于文件。
for(int fnum = 0; fnum< files.size(); fnum ++)
curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
返回curDir;
}
public Dimension getMinimumSize(){
return new Dimension(200,400);
}
public Dimension getPreferredSize(){
return new Dimension(200,400);
}
/ ** Main:制作一个Frame,添加一个FileTree * /
public static void main(String [] av){
JFrame frame = new JFrame(FileTree);
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
容器cp = frame.getContentPane();
if(av.length == 0){
cp.add(new pnl_fileView(new myFile(。)));
} else {
cp.setLayout(new BoxLayout(cp,BoxLayout.X_AXIS));
for(int i = 0; i< av.length; i ++)
cp.add(new pnl_fileView(new myFile(av [i])));
}
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class myFile extends File {
public myFile(String pathname){
super(pathname);
}
public String toString(){
returnHello World!;
}
public String getAbsolutePath(){
returnhi;
}
}
In除了 code> FileTreeModel 。
-
JTree
使用FileTreeModel
,。
- 。
In my project, I am trying to add a file explorer so the user can select files from a given directory. I want to limit this view to the project's root folder (which is determined by the user). This is very much like Eclipses Package Explorer, as the "workspace" is determined by the user.
Currently files do not display the full path (from C:) which is what I want, but all of the folders display the full path (Which I do not want, i just want the folder name).
So how does JTree display these names?
I have seen around that JTree uses the File.tostring() method, but when I implemented my own file and overwrote the toString method, nothing changed.
Here is my code:
import java.awt.BorderLayout;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.util.Collections;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
public class pnl_fileView extends JPanel {
/**
* Display a file system in a JTree view
*
* @version $Id: FileTree.java,v 1.9 2004/02/23 03:39:22 ian Exp $
* @author Ian Darwin
*/
/** Construct a FileTree */
// public pnl_fileView(){
private myFile projectFile;
public pnl_fileView(myFile dir) {
//Create file explorer
//Need to add setup for root folder change.
//Config.getProject(); //This gets the current file from the config file.
//Begin choose File
if(projectFile == null){
JFileChooser chooser;
String choosertitle = "Please Choose a Root Folder";
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//
// disable the "All files" option.
//
chooser.setAcceptAllFileFilterUsed(false);
//
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
projectFile = new myFile(chooser.getSelectedFile().getAbsolutePath());
//projectFile = chooser.getSelectedFile();
}
else {
System.out.println("No Selection ");
}
}
else {
// Figure out where in the filesystem to start displaying
}
//End choose file
setLayout(new BorderLayout());
// Make a tree list with all the nodes, and make it a JTree
JTree tree = new JTree(addNodes(null, projectFile));
// Add a listener
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e
.getPath().getLastPathComponent();
System.out.println("You selected " + node);
}
});
// Lastly, put the JTree into a JScrollPane.
JScrollPane scrollpane = new JScrollPane();
scrollpane.getViewport().add(tree);
add(BorderLayout.CENTER, scrollpane);
}
/** Add nodes from under "dir" into curTop. Highly recursive. */
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, myFile dir) {
String curPath = dir.getPath();
System.out.println(curPath);
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if (curTop != null) { // should only be null at root
curTop.add(curDir);
}
Vector<String> ol = new Vector<String>();
String[] tmp = dir.list();
for (int i = 0; i < tmp.length; i++)
ol.addElement(tmp[i]);
Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
myFile f;
Vector<String> files = new Vector<String>();
// Make two passes, one for Dirs and one for Files. This is #1.
for (int i = 0; i < ol.size(); i++) {
String thisObject = (String) ol.elementAt(i);
String newPath;
if (curPath.equals("."))
newPath = thisObject;
else
newPath = curPath + myFile.separator + thisObject;
System.out.println("this is the path: " + newPath);
if ((f = new myFile(newPath)).isDirectory())
addNodes(curDir, f);
else
files.addElement(thisObject);
}
// Pass two: for files.
for (int fnum = 0; fnum < files.size(); fnum++)
curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
return curDir;
}
public Dimension getMinimumSize() {
return new Dimension(200, 400);
}
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}
/** Main: make a Frame, add a FileTree */
public static void main(String[] av) {
JFrame frame = new JFrame("FileTree");
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
Container cp = frame.getContentPane();
if (av.length == 0) {
cp.add(new pnl_fileView(new myFile(".")));
} else {
cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));
for (int i = 0; i < av.length; i++)
cp.add(new pnl_fileView(new myFile(av[i])));
}
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class myFile extends File{
public myFile(String pathname) {
super(pathname);
}
public String toString() {
return "Hello World!";
}
public String getAbsolutePath(){
return "hi";
}
}
In addition to JFileChooser
, other examples of custom Java file explorers include these:
org.netbeans.swing.outline.Outline
withFileTreeModel
.
JTree
withFileTreeModel
, described here.
这篇关于JTree如何显示文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!