java jtree 文件目录树 怎么让目录树不显示文件夹的路径,目录树add的时候用的是File类型
import java.awt.BorderLayout;
网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了呼伦贝尔免费建站欢迎大家使用!
import java.awt.Component;
import java.io.File;
import java.util.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeNode;
/**
* @author Kirill Grouchnikov
*/
public class FileTreePanel extends JPanel {
/**
* File system view.
*/
protected static FileSystemView fsv = FileSystemView.getFileSystemView();
/**
* Renderer for the file tree.
*
* @author Kirill Grouchnikov
*/
private static class FileTreeCellRenderer extends DefaultTreeCellRenderer {
/**
* Icon cache to speed the rendering.
*/
private MapString, Icon iconCache = new HashMapString, Icon();
/**
* Root name cache to speed the rendering.
*/
private MapFile, String rootNameCache = new HashMapFile, String();
/*
* (non-Javadoc)
*
* @see javax.swing.tree.DefaultTreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree,
* java.lang.Object, boolean, boolean, boolean, int, boolean)
*/
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
FileTreeNode ftn = (FileTreeNode) value;
File file = ftn.file;
String filename = "";
if (file != null) {
if (ftn.isFileSystemRoot) {
// long start = System.currentTimeMillis();
filename = this.rootNameCache.get(file);
if (filename == null) {
filename = fsv.getSystemDisplayName(file);
this.rootNameCache.put(file, filename);
}
// long end = System.currentTimeMillis();
// System.out.println(filename + ":" + (end - start));
} else {
filename = file.getName();
}
}
JLabel result = (JLabel) super.getTreeCellRendererComponent(tree,
filename, sel, expanded, leaf, row, hasFocus);
if (file != null) {
Icon icon = this.iconCache.get(filename);
if (icon == null) {
// System.out.println("Getting icon of " + filename);
icon = fsv.getSystemIcon(file);
this.iconCache.put(filename, icon);
}
result.setIcon(icon);
}
return result;
}
}
/**
* A node in the file tree.
*
* @author Kirill Grouchnikov
*/
private static class FileTreeNode implements TreeNode {
/**
* Node file.
*/
private File file;
/**
* Children of the node file.
*/
private File[] children;
/**
* Parent node.
*/
private TreeNode parent;
/**
* Indication whether this node corresponds to a file system root.
*/
private boolean isFileSystemRoot;
/**
* Creates a new file tree node.
*
* @param file
* Node file
* @param isFileSystemRoot
* Indicates whether the file is a file system root.
* @param parent
* Parent node.
*/
public FileTreeNode(File file, boolean isFileSystemRoot, TreeNode parent) {
this.file = file;
this.isFileSystemRoot = isFileSystemRoot;
this.parent = parent;
this.children = this.file.listFiles();
if (this.children == null)
this.children = new File[0];
}
/**
* Creates a new file tree node.
*
* @param children
* Children files.
*/
public FileTreeNode(File[] children) {
this.file = null;
this.parent = null;
this.children = children;
}
/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#children()
*/
public Enumeration? children() {
final int elementCount = this.children.length;
return new EnumerationFile() {
int count = 0;
/*
* (non-Javadoc)
*
* @see java.util.Enumeration#hasMoreElements()
*/
public boolean hasMoreElements() {
return this.count elementCount;
}
/*
* (non-Javadoc)
*
* @see java.util.Enumeration#nextElement()
*/
public File nextElement() {
if (this.count elementCount) {
return FileTreeNode.this.children[this.count++];
}
throw new NoSuchElementException("Vector Enumeration");
}
};
}
/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getAllowsChildren()
*/
public boolean getAllowsChildren() {
return true;
}
/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getChildAt(int)
*/
public TreeNode getChildAt(int childIndex) {
return new FileTreeNode(this.children[childIndex],
this.parent == null, this);
}
/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getChildCount()
*/
public int getChildCount() {
return this.children.length;
}
/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getIndex(javax.swing.tree.TreeNode)
*/
public int getIndex(TreeNode node) {
FileTreeNode ftn = (FileTreeNode) node;
for (int i = 0; i this.children.length; i++) {
if (ftn.file.equals(this.children[i]))
return i;
}
return -1;
}
/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getParent()
*/
public TreeNode getParent() {
return this.parent;
}
/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#isLeaf()
*/
public boolean isLeaf() {
return (this.getChildCount() == 0);
}
}
/**
* The file tree.
*/
private JTree tree;
/**
* Creates the file tree panel.
*/
public FileTreePanel() {
this.setLayout(new BorderLayout());
File[] roots = File.listRoots();
FileTreeNode rootTreeNode = new FileTreeNode(roots);
this.tree = new JTree(rootTreeNode);
this.tree.setCellRenderer(new FileTreeCellRenderer());
this.tree.setRootVisible(false);
final JScrollPane jsp = new JScrollPane(this.tree);
jsp.setBorder(new EmptyBorder(0, 0, 0, 0));
this.add(jsp, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("File tree");
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.add(new FileTreePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
Java web工程中前台网站jsp怎样做可以隐藏路径 不是用frame那种
用URL重写试试,或者等加载完某页面后用JavaScript修改浏览器的URL,或者用服务器端包含,等等,很多情况。
关于java代码中文件路径的问题
这就是相对路径
指的是相对于工程文件的位置而言
在eclipse的结构图中的位置
在windows的文件夹里的位置
在查看属性里的绝对路径的位置
代码来找文件路径
public class Test {
public static void main(String[] args) throws Exception {
System.out.println("当前目录的路径\t"+new File(".").getCanonicalPath());// "."表示当前目录
File file = new File("Buffered.txt");
if(!file.exists()){//如果不存在,就新建该文件
file.createNewFile();
}
System.out.println("Buffered.txt的绝对路径\t"+file.getCanonicalPath());
System.out.println("Buffered.txt的相对路径\t"+file.getPath());
}
}
输出
当前目录的路径 D:\space\workspace\Demo
Buffered.txt的绝对路径 D:\space\workspace\Demo\Buffered.txt
Buffered.txt的相对路径 Buffered.txt
javaweb跳转如何能看不见后台java代码路径
有两种跳转方式,你用不需要传值的时候用的那种,那个单词我不会写了,他就不会显示你的路劲
java web 屏蔽一段代码用什么符号
JAVA里面?
//
或
/*
*/
HTML?
!--
--
当前文章:Java屏蔽路径代码 java怎么屏蔽部分代码
分享URL:http://lswzjz.com/article/dooceie.html