FileBro功能
目录树-显示文件系统在启动时的根目录,但以其他方式延迟构建用户浏览文件系统。 FileBro在加载新条目时显示进度条。
文件列表(表)显示当前目录树选择中的目录和文件的列表。通过单击列标题进行排序。
按钮-所有按钮均使用
Desktop
类来实现其功能。如果无法完成该操作,则应该在JEditorPane
错误消息中显示有意义的原因。Locate
-打开当前所选文件的父目录。 Open
-启动该文件类型的默认使用者的任何应用程序。Edit
-在默认使用者中打开文件进行编辑。 Print
-使用默认使用者打印文件。所选文件的详细信息显示在按钮下方。
当前功能是否正常工作是否按照上述说明?
FileBrowser.java
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.table.*;
import javax.swing.filechooser.FileSystemView;
import javax.imageio.ImageIO;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.nio.channels.FileChannel;
import java.net.URL;
/**
A basic File Browser. Requires 1.6+ for the Desktop & SwingWorker
classes, amongst other minor things.
Includes support classes FileTableModel & FileTreeCellRenderer.
@TODO Bugs
<li>Fix keyboard focus issues - especially when functions like
rename/delete etc. are called that update nodes & file lists.
<li>Needs more testing in general.
@TODO Functionality
<li>Double clicking a directory in the table, should update the tree
<li>Move progress bar?
<li>Add other file display modes (besides table) in CardLayout?
<li>Menus + other cruft?
<li>Implement history/back
<li>Allow multiple selection
<li>Add file search
@author Andrew Thompson
@version 2011-06-08
@see http://codereview.stackexchange.com/q/4446/7784
@license LGPL
*/
class FileBrowser {
/** Title of the application */
public static final String APP_TITLE = "FileBro";
/** Used to open/edit/print files. */
private Desktop desktop;
/** Provides nice icons and names for files. */
private FileSystemView fileSystemView;
/** currently selected File. */
private File currentFile;
/** Main GUI container */
private JPanel gui;
/** File-system tree. Built Lazily */
private JTree tree;
private DefaultTreeModel treeModel;
/** Directory listing */
private JTable table;
private JProgressBar progressBar;
/** Table model for File[]. */
private FileTableModel fileTableModel;
private ListSelectionListener listSelectionListener;
private boolean cellSizesSet = false;
private int rowIconPadding = 6;
/* File controls. */
private JButton openFile;
private JButton printFile;
private JButton editFile;
/* File details. */
private JLabel fileName;
private JTextField path;
private JLabel date;
private JLabel size;
private JCheckBox readable;
private JCheckBox writable;
private JCheckBox executable;
private JRadioButton isDirectory;
private JRadioButton isFile;
/* GUI options/containers for new File/Directory creation. Created lazily. */
private JPanel newFilePanel;
private JRadioButton newTypeFile;
private JTextField name;
public Container getGui() {
if (gui==null) {
gui = new JPanel(new BorderLayout(3,3));
gui.setBorder(new EmptyBorder(5,5,5,5));
fileSystemView = FileSystemView.getFileSystemView();
desktop = Desktop.getDesktop();
JPanel detailView = new JPanel(new BorderLayout(3,3));
table = new JTable();
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoCreateRowSorter(true);
table.setShowVerticalLines(false);
listSelectionListener = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent lse) {
int row = table.getSelectionModel().getLeadSelectionIndex();
setFileDetails( ((FileTableModel)table.getModel()).getFile(row) );
}
};
table.getSelectionModel().addListSelectionListener(listSelectionListener);
JScrollPane tableScroll = new JScrollPane(table);
Dimension d = tableScroll.getPreferredSize();
tableScroll.setPreferredSize(new Dimension((int)d.getWidth(), (int)d.getHeight()/2));
detailView.add(tableScroll, BorderLayout.CENTER);
// the File tree
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
treeModel = new DefaultTreeModel(root);
TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent tse){
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)tse.getPath().getLastPathComponent();
showChildren(node);
setFileDetails((File)node.getUserObject());
}
};
// show the file system roots.
File[] roots = fileSystemView.getRoots();
for (File fileSystemRoot : roots) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(fileSystemRoot);
root.add( node );
File[] files = fileSystemView.getFiles(fileSystemRoot, true);
for (File file : files) {
if (file.isDirectory()) {
node.add(new DefaultMutableTreeNode(file));
}
}
//
}
tree = new JTree(treeModel);
tree.setRootVisible(false);
tree.addTreeSelectionListener(treeSelectionListener);
tree.setCellRenderer(new FileTreeCellRenderer());
tree.expandRow(0);
JScrollPane treeScroll = new JScrollPane(tree);
// as per trashgod tip
tree.setVisibleRowCount(15);
Dimension preferredSize = treeScroll.getPreferredSize();
Dimension widePreferred = new Dimension(
200,
(int)preferredSize.getHeight());
treeScroll.setPreferredSize( widePreferred );
// details for a File
JPanel fileMainDetails = new JPanel(new BorderLayout(4,2));
fileMainDetails.setBorder(new EmptyBorder(0,6,0,6));
JPanel fileDetailsLabels = new JPanel(new GridLayout(0,1,2,2));
fileMainDetails.add(fileDetailsLabels, BorderLayout.WEST);
JPanel fileDetailsValues = new JPanel(new GridLayout(0,1,2,2));
fileMainDetails.add(fileDetailsValues, BorderLayout.CENTER);
fileDetailsLabels.add(new JLabel("File", JLabel.TRAILING));
fileName = new JLabel();
fileDetailsValues.add(fileName);
fileDetailsLabels.add(new JLabel("Path/name", JLabel.TRAILING));
path = new JTextField(5);
path.setEditable(false);
fileDetailsValues.add(path);
fileDetailsLabels.add(new JLabel("Last Modified", JLabel.TRAILING));
date = new JLabel();
fileDetailsValues.add(date);
fileDetailsLabels.add(new JLabel("File size", JLabel.TRAILING));
size = new JLabel();
fileDetailsValues.add(size);
fileDetailsLabels.add(new JLabel("Type", JLabel.TRAILING));
JPanel flags = new JPanel(new FlowLayout(FlowLayout.LEADING,4,0));
isDirectory = new JRadioButton("Directory");
flags.add(isDirectory);
isFile = new JRadioButton("File");
flags.add(isFile);
fileDetailsValues.add(flags);
JToolBar toolBar = new JToolBar();
// mnemonics stop working in a floated toolbar
toolBar.setFloatable(false);
JButton locateFile = new JButton("Locate");
locateFile.setMnemonic('l');
locateFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
try {
System.out.println("Locate: " + currentFile.getParentFile());
desktop.open(currentFile.getParentFile());
} catch(Throwable t) {
showThrowable(t);
}
gui.repaint();
}
});
toolBar.add(locateFile);
openFile = new JButton("Open");
openFile.setMnemonic('o');
openFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
try {
System.out.println("Open: " + currentFile);
desktop.open(currentFile);
} catch(Throwable t) {
showThrowable(t);
}
gui.repaint();
}
});
toolBar.add(openFile);
editFile = new JButton("Edit");
editFile.setMnemonic('e');
editFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
try {
desktop.edit(currentFile);
} catch(Throwable t) {
showThrowable(t);
}
}
});
toolBar.add(editFile);
printFile = new JButton("Print");
printFile.setMnemonic('p');
printFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
try {
desktop.print(currentFile);
} catch(Throwable t) {
showThrowable(t);
}
}
});
toolBar.add(printFile);
// Check the actions are supported on this platform!
openFile.setEnabled(desktop.isSupported(Desktop.Action.OPEN));
editFile.setEnabled(desktop.isSupported(Desktop.Action.EDIT));
printFile.setEnabled(desktop.isSupported(Desktop.Action.PRINT));
flags.add(new JLabel(":: Flags"));
readable = new JCheckBox("Read ");
readable.setMnemonic('a');
flags.add(readable);
writable = new JCheckBox("Write ");
writable.setMnemonic('w');
flags.add(writable);
executable = new JCheckBox("Execute");
executable.setMnemonic('x');
flags.add(executable);
int count = fileDetailsLabels.getComponentCount();
for (int ii=0; ii<count; ii++) {
fileDetailsLabels.getComponent(ii).setEnabled(false);
}
count = flags.getComponentCount();
for (int ii=0; ii<count; ii++) {
flags.getComponent(ii).setEnabled(false);
}
JPanel fileView = new JPanel(new BorderLayout(3,3));
fileView.add(toolBar,BorderLayout.NORTH);
fileView.add(fileMainDetails,BorderLayout.CENTER);
detailView.add(fileView, BorderLayout.SOUTH);
JSplitPane splitPane = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT,
treeScroll,
detailView);
gui.add(splitPane, BorderLayout.CENTER);
JPanel simpleOutput = new JPanel(new BorderLayout(3,3));
progressBar = new JProgressBar();
simpleOutput.add(progressBar, BorderLayout.EAST);
progressBar.setVisible(false);
gui.add(simpleOutput, BorderLayout.SOUTH);
}
return gui;
}
public void showRootFile() {
// ensure the main files are displayed
tree.setSelectionInterval(0,0);
}
private TreePath findTreePath(File find) {
for (int ii=0; ii<tree.getRowCount(); ii++) {
TreePath treePath = tree.getPathForRow(ii);
Object object = treePath.getLastPathComponent();
DefaultMutableTreeNode node = (DefaultMutableTreeNode)object;
File nodeFile = (File)node.getUserObject();
if (nodeFile==find) {
return treePath;
}
}
// not found!
return null;
}
private void showErrorMessage(String errorMessage, String errorTitle) {
JOptionPane.showMessageDialog(
gui,
errorMessage,
errorTitle,
JOptionPane.ERROR_MESSAGE
);
}
private void showThrowable(Throwable t) {
t.printStackTrace();
JOptionPane.showMessageDialog(
gui,
t.toString(),
t.getMessage(),
JOptionPane.ERROR_MESSAGE
);
gui.repaint();
}
/** Update the table on the EDT */
private void setTableData(final File[] files) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (fileTableModel==null) {
fileTableModel = new FileTableModel();
table.setModel(fileTableModel);
}
table.getSelectionModel().removeListSelectionListener(listSelectionListener);
fileTableModel.setFiles(files);
table.getSelectionModel().addListSelectionListener(listSelectionListener);
if (!cellSizesSet) {
Icon icon = fileSystemView.getSystemIcon(files[0]);
// size adjustment to better account for icons
table.setRowHeight( icon.getIconHeight()+rowIconPadding );
setColumnWidth(0,-1);
setColumnWidth(3,60);
table.getColumnModel().getColumn(3).setMaxWidth(120);
setColumnWidth(4,-1);
setColumnWidth(5,-1);
setColumnWidth(6,-1);
setColumnWidth(7,-1);
setColumnWidth(8,-1);
setColumnWidth(9,-1);
cellSizesSet = true;
}
}
});
}
private void setColumnWidth(int column, int width) {
TableColumn tableColumn = table.getColumnModel().getColumn(column);
if (width<0) {
// use the preferred width of the header..
JLabel label = new JLabel( (String)tableColumn.getHeaderValue() );
Dimension preferred = label.getPreferredSize();
// altered 10->14 as per camickr comment.
width = (int)preferred.getWidth()+14;
}
tableColumn.setPreferredWidth(width);
tableColumn.setMaxWidth(width);
tableColumn.setMinWidth(width);
}
/** Add the files that are contained within the directory of this node.
Thanks to Hovercraft Full Of Eels for the SwingWorker fix. */
private void showChildren(final DefaultMutableTreeNode node) {
tree.setEnabled(false);
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
SwingWorker<Void, File> worker = new SwingWorker<Void, File>() {
@Override
public Void doInBackground() {
File file = (File) node.getUserObject();
if (file.isDirectory()) {
File[] files = fileSystemView.getFiles(file, true); //!!
if (node.isLeaf()) {
for (File child : files) {
if (child.isDirectory()) {
publish(child);
}
}
}
setTableData(files);
}
return null;
}
@Override
protected void process(List<File> chunks) {
for (File child : chunks) {
node.add(new DefaultMutableTreeNode(child));
}
}
@Override
protected void done() {
progressBar.setIndeterminate(false);
progressBar.setVisible(false);
tree.setEnabled(true);
}
};
worker.execute();
}
/** Update the File details view with the details of this File. */
private void setFileDetails(File file) {
currentFile = file;
Icon icon = fileSystemView.getSystemIcon(file);
fileName.setIcon(icon);
fileName.setText(fileSystemView.getSystemDisplayName(file));
path.setText(file.getPath());
date.setText(new Date(file.lastModified()).toString());
size.setText(file.length() + " bytes");
readable.setSelected(file.canRead());
writable.setSelected(file.canWrite());
executable.setSelected(file.canExecute());
isDirectory.setSelected(file.isDirectory());
isFile.setSelected(file.isFile());
JFrame f = (JFrame)gui.getTopLevelAncestor();
if (f!=null) {
f.setTitle(
APP_TITLE +
" :: " +
fileSystemView.getSystemDisplayName(file) );
}
gui.repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
// Significantly improves the look of the output in
// terms of the file names returned by FileSystemView!
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception weTried) {
}
JFrame f = new JFrame(APP_TITLE);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FileBrowser FileBrowser = new FileBrowser();
f.setContentPane(FileBrowser.getGui());
try {
URL urlBig = FileBrowser.getClass().getResource("fb-icon-32x32.png");
URL urlSmall = FileBrowser.getClass().getResource("fb-icon-16x16.png");
ArrayList<Image> images = new ArrayList<Image>();
images.add( ImageIO.read(urlBig) );
images.add( ImageIO.read(urlSmall) );
f.setIconImages(images);
} catch(Exception weTried) {}
f.pack();
f.setLocationByPlatform(true);
f.setMinimumSize(f.getSize());
f.setVisible(true);
FileBrowser.showRootFile();
}
});
}
}
/** A TableModel to hold File[]. */
class FileTableModel extends AbstractTableModel {
private File[] files;
private FileSystemView fileSystemView = FileSystemView.getFileSystemView();
private String[] columns = {
"Icon",
"File",
"Path/name",
"Size",
"Last Modified",
"R",
"W",
"E",
"D",
"F",
};
FileTableModel() {
this(new File[0]);
}
FileTableModel(File[] files) {
this.files = files;
}
public Object getValueAt(int row, int column) {
File file = files[row];
switch (column) {
case 0:
return fileSystemView.getSystemIcon(file);
case 1:
return fileSystemView.getSystemDisplayName(file);
case 2:
return file.getPath();
case 3:
return file.length();
case 4:
return file.lastModified();
case 5:
return file.canRead();
case 6:
return file.canWrite();
case 7:
return file.canExecute();
case 8:
return file.isDirectory();
case 9:
return file.isFile();
default:
System.err.println("Logic Error");
}
return "";
}
public int getColumnCount() {
return columns.length;
}
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return ImageIcon.class;
case 3:
return Long.class;
case 4:
return Date.class;
case 5:
case 6:
case 7:
case 8:
case 9:
return Boolean.class;
}
return String.class;
}
public String getColumnName(int column) {
return columns[column];
}
public int getRowCount() {
return files.length;
}
public File getFile(int row) {
return files[row];
}
public void setFiles(File[] files) {
this.files = files;
fireTableDataChanged();
}
}
/** A TreeCellRenderer for a File. */
class FileTreeCellRenderer extends DefaultTreeCellRenderer {
private FileSystemView fileSystemView;
private JLabel label;
FileTreeCellRenderer() {
label = new JLabel();
label.setOpaque(true);
fileSystemView = FileSystemView.getFileSystemView();
}
@Override
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
File file = (File)node.getUserObject();
label.setIcon(fileSystemView.getSystemIcon(file));
label.setText(fileSystemView.getSystemDisplayName(file));
label.setToolTipText(file.getPath());
if (selected) {
label.setBackground(backgroundSelectionColor);
label.setForeground(textSelectionColor);
} else {
label.setBackground(backgroundNonSelectionColor);
label.setForeground(textNonSelectionColor);
}
return label;
}
}
FileBrowser.java
源代码约为650行。另外,
这里是该应用的2个图标。 (但是没有它们,它将运行正常)。如果在与类相同的目录中找到它们,则将使用它们。这些图标使用
Color.WHITE
表示未选择的表行和树节点,因此在带有白色背景的网页中不可见。 ;)fb-icon-16x16.png
fb-icon-32x32.png
欢迎其他任何评论/提示/建议。
#1 楼
这段代码与我有关: SwingWorker worker = new SwingWorker() {
@Override
public String doInBackground() {
tree.setEnabled(false);
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
File file = (File) node.getUserObject();
if (file.isDirectory()) {
File[] files = fileSystemView.getFiles(file, true);
if (node.isLeaf()) {
for (File child : files) {
if (child.isDirectory()) {
node.add(new DefaultMutableTreeNode(child));
}
}
}
setTableData(files);
}
progressBar.setIndeterminate(false);
progressBar.setVisible(false);
tree.setEnabled(true);
return "done";
}
};
worker.execute();
,因为您正在对EDT之外的JProgressBar进行Swing调用。最好先在SwingWorker之前启动进度条,然后在done方法中结束它。要么添加一个PropertyChangeListener到SwingWorker上,然后当worker的state属性为StateValue.DONE时结束进度条。使用此函数时必须注意,因为您似乎确实在多个线程中使用此函数:
这不是线程安全类。如果要使用DefaultMutableTreeNode(或在一个以上的线程中,您需要自己进行同步。一个好的约定是在树的根节点上进行同步。
EDIT
至少可以从等式中获取DefaultMutableTreeNode的一种方法是向其添加节点通过使用SwingWorker的发布/处理,仅在一个线程中,即EDT。例如:
private void showChildren(final DefaultMutableTreeNode node) {
tree.setEnabled(false);
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
SwingWorker<Void, File> worker = new SwingWorker<Void, File>() {
@Override
public Void doInBackground() {
File file = (File) node.getUserObject();
if (file.isDirectory()) {
File[] files = fileSystemView.getFiles(file, true); //!!
if (node.isLeaf()) {
for (File child : files) {
if (child.isDirectory()) {
publish(child);
}
}
}
setTableData(files);
}
return null;
}
@Override
protected void process(List<File> chunks) {
for (File child : chunks) {
node.add(new DefaultMutableTreeNode(child));
}
}
@Override
protected void done() {
progressBar.setIndeterminate(false);
progressBar.setVisible(false);
tree.setEnabled(true);
}
};
worker.execute();
}
评论
\ $ \ begingroup \ $
这是对fileSystemView.getFiles(file,true);的调用。这就是问题所在-已关闭电源的驱动器或“网络”文件夹需要一些时间。可以说,如果我不应该在doInBG()方法中更新GUI元素,那也就不应该在树中添加节点。对?还是最后一段。 &引用解决该问题?
\ $ \ endgroup \ $
–安德鲁·汤普森(Andrew Thompson)
2011年5月31日下午2:28
\ $ \ begingroup \ $
@Andrew:请查看上面对我的帖子的编辑,其中使用了更新的SwingWorker,它使用发布和处理功能来添加到EDT的节点上。
\ $ \ endgroup \ $
–气垫船充满鳗鱼
2011年5月31日下午2:33
\ $ \ begingroup \ $
对,只能在EDT上更改模型。
\ $ \ endgroup \ $
–垃圾神
2011年5月31日下午2:56
\ $ \ begingroup \ $
@AndrewThompson:另外,发生错误时,您是否还在记录程序的状态?消息中是否有任何有用的信息?
\ $ \ endgroup \ $
–气垫船充满鳗鱼
2011年5月31日,下午3:12
\ $ \ begingroup \ $
@HFOE:对SwingWorker实施了更加严格的处理。此后没有看到任何NPE(手指交叉)。请参阅编辑以获取最新代码。至于堆栈跟踪,它们从未在FileManager.java中指示一行,它们很少出现,以至于我常常只知道它们是由于CLI从关闭的应用程序后面消失而发生的。始终在桌子上放置AIOOBE或在桌子或树上放置NPE。我怀疑他们现在已经排序了。
\ $ \ endgroup \ $
–安德鲁·汤普森(Andrew Thompson)
2011年5月31日上午10:10
#2 楼
Mac OS X 10.5和Ubuntu 10.04:删除时重复非致命NPE。附录:在441行中,
findTreePath()
始终返回null
,因为currentFile.getParentFile()
返回文件的路径,而不是文件本身。实际上,路径无法匹配自身。我看不到明显的解决方法。附录:我找不到可接受的解决方案。我怀疑问题围绕视图(
JTree
)和模型(DefaultTreeModel
)之间的二分法展开。特别是,findTreePath()
正在可见行中搜索而不是在树模型的节点中搜索。如本TreeModel
所示,明确实现FileTreeModel
可能会很有用。它也可能有助于抽象类似于JTable
提供的视图模型转换方法。作为替代方法,请考虑下图所示的
org.netbeans.swing.outline.Outline
。它接受前面提到的FileTreeModel
。通过扩展JTable
,它通过类似于RowModel
的便捷TableModel
接口使用熟悉的渲染器和编辑器架构。最重要的是,它提供convertRowIndexToModel()
以及convertRowIndexToView()
。 JTable
方法getValueAt()
被重写以调用convertRowIndexToModel()
,从而使本示例中的选择侦听器变得简单。 JAR独立,小(〜230 KiB)且稳定;可以在NetBeans发行版中找到它:NetBeans/platform/modules/org-netbeans-swing-outline.jar
附录:
FileBrowser
版本2011-06 -08在上面引用的两个系统上都能正常运行。currentFile: /temp.txt parentPath: null java.lang.NullPointerException at FileManager.deleteFile(FileManager.java:443) at FileManager.access00(FileManager.java:56) at FileManager.actionPerformed(FileManager.java:306) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6374) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6139) at java.awt.Container.processEvent(Container.java:2085) at java.awt.Component.dispatchEventImpl(Component.java:4736) at java.awt.Container.dispatchEventImpl(Container.java:2143) at java.awt.Component.dispatchEvent(Component.java:4566) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212) at java.awt.Container.dispatchEventImpl(Container.java:2129) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4566) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:680) at java.awt.EventQueue.accessq4312078q0(EventQueue.java:86) at java.awt.EventQueue.run(EventQueue.java:639) at java.awt.EventQueue.run(EventQueue.java:637) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext.doIntersectionPrivilege(AccessControlContext.java:87) at java.security.AccessControlContext.doIntersectionPrivilege(AccessControlContext.java:98) at java.awt.EventQueue.run(EventQueue.java:653) at java.awt.EventQueue.run(EventQueue.java:651) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:650) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
评论
\ $ \ begingroup \ $
我从未见过动态更改框架图标的应用程序。如果您尝试通过任务栏上的图标查找应用程序,或者使用Alt-tab查找应用程序,这不会造成混淆。我建议您可以只创建一个FileManager图标,而不要对其进行更改。
\ $ \ endgroup \ $
– camickr
2011年5月31日下午4:43
\ $ \ begingroup \ $
另请参阅PanelBrowser。
\ $ \ endgroup \ $
–垃圾神
13年2月27日在5:13
#3 楼
这是在我决定将FileMan(ager)转换为FileBro(wser)之前对代码的最后一次编辑(FileBro将对原始问题进行编辑,“现在就真正开始”)。此版本的FileMan张贴在这里,是为希望向该方向扩展此代码(非常不完整,有错误的代码)的人们提供帮助。 br />import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.table.*;
import javax.swing.filechooser.FileSystemView;
import javax.imageio.ImageIO;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.nio.channels.FileChannel;
import java.net.URL;
/**
A basic File Manager. Requires 1.6+ for the Desktop & SwingWorker
classes, amongst other minor things.
Includes support classes FileTableModel & FileTreeCellRenderer.
@TODO Bugs
<li>Still throws occasional AIOOBEs and NPEs, so some update on
the EDT must have been missed.
<li>Fix keyboard focus issues - especially when functions like
rename/delete etc. are called that update nodes & file lists.
<li>Needs more testing in general.
@TODO Functionality
<li>Implement Read/Write/Execute checkboxes
<li>Implement Copy
<li>Extra prompt for directory delete (camickr suggestion)
<li>Add File/Directory fields to FileTableModel
<li>Double clicking a directory in the table, should update the tree
<li>Move progress bar?
<li>Add other file display modes (besides table) in CardLayout?
<li>Menus + other cruft?
<li>Implement history/back
<li>Allow multiple selection
<li>Add file search
@author Andrew Thompson
@version 2011-06-01
@see http://stackoverflow.com/questions/6182110
@license LGPL
*/
class FileManager {
/** Title of the application */
public static final String APP_TITLE = "FileMan";
/** Used to open/edit/print files. */
private Desktop desktop;
/** Provides nice icons and names for files. */
private FileSystemView fileSystemView;
/** currently selected File. */
private File currentFile;
/** Main GUI container */
private JPanel gui;
/** File-system tree. Built Lazily */
private JTree tree;
private DefaultTreeModel treeModel;
/** Directory listing */
private JTable table;
private JProgressBar progressBar;
/** Table model for File[]. */
private FileTableModel fileTableModel;
private ListSelectionListener listSelectionListener;
private boolean cellSizesSet = false;
private int rowIconPadding = 6;
/* File controls. */
private JButton openFile;
private JButton printFile;
private JButton editFile;
private JButton deleteFile;
private JButton newFile;
private JButton copyFile;
/* File details. */
private JLabel fileName;
private JTextField path;
private JLabel date;
private JLabel size;
private JCheckBox readable;
private JCheckBox writable;
private JCheckBox executable;
private JRadioButton isDirectory;
private JRadioButton isFile;
/* GUI options/containers for new File/Directory creation. Created lazily. */
private JPanel newFilePanel;
private JRadioButton newTypeFile;
private JTextField name;
public Container getGui() {
if (gui==null) {
gui = new JPanel(new BorderLayout(3,3));
gui.setBorder(new EmptyBorder(5,5,5,5));
fileSystemView = FileSystemView.getFileSystemView();
desktop = Desktop.getDesktop();
JPanel detailView = new JPanel(new BorderLayout(3,3));
//fileTableModel = new FileTableModel();
table = new JTable();
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoCreateRowSorter(true);
table.setShowVerticalLines(false);
listSelectionListener = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent lse) {
int row = table.getSelectionModel().getLeadSelectionIndex();
setFileDetails( ((FileTableModel)table.getModel()).getFile(row) );
}
};
table.getSelectionModel().addListSelectionListener(listSelectionListener);
JScrollPane tableScroll = new JScrollPane(table);
Dimension d = tableScroll.getPreferredSize();
tableScroll.setPreferredSize(new Dimension((int)d.getWidth(), (int)d.getHeight()/2));
detailView.add(tableScroll, BorderLayout.CENTER);
// the File tree
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
treeModel = new DefaultTreeModel(root);
TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent tse){
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)tse.getPath().getLastPathComponent();
showChildren(node);
setFileDetails((File)node.getUserObject());
}
};
// show the file system roots.
File[] roots = fileSystemView.getRoots();
for (File fileSystemRoot : roots) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(fileSystemRoot);
root.add( node );
//showChildren(node);
//
File[] files = fileSystemView.getFiles(fileSystemRoot, true);
for (File file : files) {
if (file.isDirectory()) {
node.add(new DefaultMutableTreeNode(file));
}
}
//
}
tree = new JTree(treeModel);
tree.setRootVisible(false);
tree.addTreeSelectionListener(treeSelectionListener);
tree.setCellRenderer(new FileTreeCellRenderer());
tree.expandRow(0);
JScrollPane treeScroll = new JScrollPane(tree);
// as per trashgod tip
tree.setVisibleRowCount(15);
Dimension preferredSize = treeScroll.getPreferredSize();
Dimension widePreferred = new Dimension(
200,
(int)preferredSize.getHeight());
treeScroll.setPreferredSize( widePreferred );
// details for a File
JPanel fileMainDetails = new JPanel(new BorderLayout(4,2));
fileMainDetails.setBorder(new EmptyBorder(0,6,0,6));
JPanel fileDetailsLabels = new JPanel(new GridLayout(0,1,2,2));
fileMainDetails.add(fileDetailsLabels, BorderLayout.WEST);
JPanel fileDetailsValues = new JPanel(new GridLayout(0,1,2,2));
fileMainDetails.add(fileDetailsValues, BorderLayout.CENTER);
fileDetailsLabels.add(new JLabel("File", JLabel.TRAILING));
fileName = new JLabel();
fileDetailsValues.add(fileName);
fileDetailsLabels.add(new JLabel("Path/name", JLabel.TRAILING));
path = new JTextField(5);
path.setEditable(false);
fileDetailsValues.add(path);
fileDetailsLabels.add(new JLabel("Last Modified", JLabel.TRAILING));
date = new JLabel();
fileDetailsValues.add(date);
fileDetailsLabels.add(new JLabel("File size", JLabel.TRAILING));
size = new JLabel();
fileDetailsValues.add(size);
fileDetailsLabels.add(new JLabel("Type", JLabel.TRAILING));
JPanel flags = new JPanel(new FlowLayout(FlowLayout.LEADING,4,0));
isDirectory = new JRadioButton("Directory");
isDirectory.setEnabled(false);
flags.add(isDirectory);
isFile = new JRadioButton("File");
isFile.setEnabled(false);
flags.add(isFile);
fileDetailsValues.add(flags);
int count = fileDetailsLabels.getComponentCount();
for (int ii=0; ii<count; ii++) {
fileDetailsLabels.getComponent(ii).setEnabled(false);
}
JToolBar toolBar = new JToolBar();
// mnemonics stop working in a floated toolbar
toolBar.setFloatable(false);
openFile = new JButton("Open");
openFile.setMnemonic('o');
openFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
try {
desktop.open(currentFile);
} catch(Throwable t) {
showThrowable(t);
}
gui.repaint();
}
});
toolBar.add(openFile);
editFile = new JButton("Edit");
editFile.setMnemonic('e');
editFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
try {
desktop.edit(currentFile);
} catch(Throwable t) {
showThrowable(t);
}
}
});
toolBar.add(editFile);
printFile = new JButton("Print");
printFile.setMnemonic('p');
printFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
try {
desktop.print(currentFile);
} catch(Throwable t) {
showThrowable(t);
}
}
});
toolBar.add(printFile);
// Check the actions are supported on this platform!
openFile.setEnabled(desktop.isSupported(Desktop.Action.OPEN));
editFile.setEnabled(desktop.isSupported(Desktop.Action.EDIT));
printFile.setEnabled(desktop.isSupported(Desktop.Action.PRINT));
toolBar.addSeparator();
newFile = new JButton("New");
newFile.setMnemonic('n');
newFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
newFile();
}
});
toolBar.add(newFile);
copyFile = new JButton("Copy");
copyFile.setMnemonic('c');
copyFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
showErrorMessage("'Copy' not implemented.", "Not implemented.");
}
});
toolBar.add(copyFile);
JButton renameFile = new JButton("Rename");
renameFile.setMnemonic('r');
renameFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
renameFile();
}
});
toolBar.add(renameFile);
deleteFile = new JButton("Delete");
deleteFile.setMnemonic('d');
deleteFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
deleteFile();
}
});
toolBar.add(deleteFile);
toolBar.addSeparator();
readable = new JCheckBox("Read ");
readable.setMnemonic('a');
//readable.setEnabled(false);
toolBar.add(readable);
writable = new JCheckBox("Write ");
writable.setMnemonic('w');
//writable.setEnabled(false);
toolBar.add(writable);
executable = new JCheckBox("Execute");
executable.setMnemonic('x');
//executable.setEnabled(false);
toolBar.add(executable);
JPanel fileView = new JPanel(new BorderLayout(3,3));
fileView.add(toolBar,BorderLayout.NORTH);
fileView.add(fileMainDetails,BorderLayout.CENTER);
detailView.add(fileView, BorderLayout.SOUTH);
JSplitPane splitPane = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT,
treeScroll,
detailView);
gui.add(splitPane, BorderLayout.CENTER);
JPanel simpleOutput = new JPanel(new BorderLayout(3,3));
progressBar = new JProgressBar();
simpleOutput.add(progressBar, BorderLayout.EAST);
progressBar.setVisible(false);
gui.add(simpleOutput, BorderLayout.SOUTH);
}
return gui;
}
public void showRootFile() {
// ensure the main files are displayed
tree.setSelectionInterval(0,0);
}
private TreePath findTreePath(File find) {
for (int ii=0; ii<tree.getRowCount(); ii++) {
TreePath treePath = tree.getPathForRow(ii);
Object object = treePath.getLastPathComponent();
DefaultMutableTreeNode node = (DefaultMutableTreeNode)object;
File nodeFile = (File)node.getUserObject();
if (nodeFile==find) {
return treePath;
}
}
// not found!
return null;
}
private void renameFile() {
if (currentFile==null) {
showErrorMessage("No file selected to rename.","Select File");
return;
}
String renameTo = JOptionPane.showInputDialog(gui, "New Name");
if (renameTo!=null) {
try {
boolean directory = currentFile.isDirectory();
TreePath parentPath = findTreePath(currentFile.getParentFile());
DefaultMutableTreeNode parentNode =
(DefaultMutableTreeNode)parentPath.getLastPathComponent();
boolean renamed = currentFile.renameTo(new File(
currentFile.getParentFile(), renameTo));
if (renamed) {
if (directory) {
// rename the node..
// delete the current node..
TreePath currentPath = findTreePath(currentFile);
System.out.println(currentPath);
DefaultMutableTreeNode currentNode =
(DefaultMutableTreeNode)currentPath.getLastPathComponent();
treeModel.removeNodeFromParent(currentNode);
// add a new node..
}
showChildren(parentNode);
} else {
String msg = "The file '" +
currentFile +
"' could not be renamed.";
showErrorMessage(msg,"Rename Failed");
}
} catch(Throwable t) {
showThrowable(t);
}
}
gui.repaint();
}
private void deleteFile() {
if (currentFile==null) {
showErrorMessage("No file selected for deletion.","Select File");
return;
}
int result = JOptionPane.showConfirmDialog(
gui,
"Are you sure you want to delete this file?",
"Delete File",
JOptionPane.ERROR_MESSAGE
);
if (result==JOptionPane.OK_OPTION) {
try {
System.out.println("currentFile: " + currentFile);
TreePath parentPath = findTreePath(currentFile.getParentFile());
System.out.println("parentPath: " + parentPath);
DefaultMutableTreeNode parentNode =
(DefaultMutableTreeNode)parentPath.getLastPathComponent();
System.out.println("parentNode: " + parentNode);
boolean directory = currentFile.isDirectory();
boolean deleted = currentFile.delete();
if (deleted) {
if (directory) {
// delete the node..
TreePath currentPath = findTreePath(currentFile);
System.out.println(currentPath);
DefaultMutableTreeNode currentNode =
(DefaultMutableTreeNode)currentPath.getLastPathComponent();
treeModel.removeNodeFromParent(currentNode);
}
showChildren(parentNode);
} else {
String msg = "The file '" +
currentFile +
"' could not be deleted.";
showErrorMessage(msg,"Delete Failed");
}
} catch(Throwable t) {
showThrowable(t);
}
}
gui.repaint();
}
private void newFile() {
if (currentFile==null) {
showErrorMessage("No location selected for new file.","Select Location");
return;
}
if (newFilePanel==null) {
newFilePanel = new JPanel(new BorderLayout(3,3));
JPanel southRadio = new JPanel(new GridLayout(1,0,2,2));
newTypeFile = new JRadioButton("File", true);
JRadioButton newTypeDirectory = new JRadioButton("Directory");
ButtonGroup bg = new ButtonGroup();
bg.add(newTypeFile);
bg.add(newTypeDirectory);
southRadio.add( newTypeFile );
southRadio.add( newTypeDirectory );
name = new JTextField(15);
newFilePanel.add( new JLabel("Name"), BorderLayout.WEST );
newFilePanel.add( name );
newFilePanel.add( southRadio, BorderLayout.SOUTH );
}
int result = JOptionPane.showConfirmDialog(
gui,
newFilePanel,
"Create File",
JOptionPane.OK_CANCEL_OPTION);
if (result==JOptionPane.OK_OPTION) {
try {
boolean created;
File parentFile = currentFile;
if (!parentFile.isDirectory()) {
parentFile = parentFile.getParentFile();
}
File file = new File( parentFile, name.getText() );
if (newTypeFile.isSelected()) {
created = file.createNewFile();
} else {
created = file.mkdir();
}
if (created) {
TreePath parentPath = findTreePath(parentFile);
DefaultMutableTreeNode parentNode =
(DefaultMutableTreeNode)parentPath.getLastPathComponent();
if (file.isDirectory()) {
// add the new node..
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(file);
TreePath currentPath = findTreePath(currentFile);
DefaultMutableTreeNode currentNode =
(DefaultMutableTreeNode)currentPath.getLastPathComponent();
treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
}
showChildren(parentNode);
} else {
String msg = "The file '" +
file +
"' could not be created.";
showErrorMessage(msg, "Create Failed");
}
} catch(Throwable t) {
showThrowable(t);
}
}
gui.repaint();
}
private void showErrorMessage(String errorMessage, String errorTitle) {
JOptionPane.showMessageDialog(
gui,
errorMessage,
errorTitle,
JOptionPane.ERROR_MESSAGE
);
}
private void showThrowable(Throwable t) {
t.printStackTrace();
JOptionPane.showMessageDialog(
gui,
t.toString(),
t.getMessage(),
JOptionPane.ERROR_MESSAGE
);
gui.repaint();
}
/** Update the table on the EDT */
private void setTableData(final File[] files) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (fileTableModel==null) {
fileTableModel = new FileTableModel();
table.setModel(fileTableModel);
}
table.getSelectionModel().removeListSelectionListener(listSelectionListener);
fileTableModel.setFiles(files);
table.getSelectionModel().addListSelectionListener(listSelectionListener);
if (!cellSizesSet) {
Icon icon = fileSystemView.getSystemIcon(files[0]);
// size adjustment to better account for icons
table.setRowHeight( icon.getIconHeight()+rowIconPadding );
setColumnWidth(0,-1);
setColumnWidth(3,60);
table.getColumnModel().getColumn(3).setMaxWidth(120);
setColumnWidth(4,-1);
setColumnWidth(5,-1);
setColumnWidth(6,-1);
setColumnWidth(7,-1);
setColumnWidth(8,-1);
setColumnWidth(9,-1);
cellSizesSet = true;
}
}
});
}
private void setColumnWidth(int column, int width) {
TableColumn tableColumn = table.getColumnModel().getColumn(column);
if (width<0) {
// use the preferred width of the header..
JLabel label = new JLabel( (String)tableColumn.getHeaderValue() );
Dimension preferred = label.getPreferredSize();
// altered 10->14 as per camickr comment.
width = (int)preferred.getWidth()+14;
}
tableColumn.setPreferredWidth(width);
tableColumn.setMaxWidth(width);
tableColumn.setMinWidth(width);
}
/** Add the files that are contained within the directory of this node.
Thanks to Hovercraft Full Of Eels. */
private void showChildren(final DefaultMutableTreeNode node) {
tree.setEnabled(false);
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
SwingWorker<Void, File> worker = new SwingWorker<Void, File>() {
@Override
public Void doInBackground() {
File file = (File) node.getUserObject();
if (file.isDirectory()) {
File[] files = fileSystemView.getFiles(file, true); //!!
if (node.isLeaf()) {
for (File child : files) {
if (child.isDirectory()) {
publish(child);
}
}
}
setTableData(files);
}
return null;
}
@Override
protected void process(List<File> chunks) {
for (File child : chunks) {
node.add(new DefaultMutableTreeNode(child));
}
}
@Override
protected void done() {
progressBar.setIndeterminate(false);
progressBar.setVisible(false);
tree.setEnabled(true);
}
};
worker.execute();
}
/** Update the File details view with the details of this File. */
private void setFileDetails(File file) {
currentFile = file;
Icon icon = fileSystemView.getSystemIcon(file);
fileName.setIcon(icon);
fileName.setText(fileSystemView.getSystemDisplayName(file));
path.setText(file.getPath());
date.setText(new Date(file.lastModified()).toString());
size.setText(file.length() + " bytes");
readable.setSelected(file.canRead());
writable.setSelected(file.canWrite());
executable.setSelected(file.canExecute());
isDirectory.setSelected(file.isDirectory());
isFile.setSelected(file.isFile());
JFrame f = (JFrame)gui.getTopLevelAncestor();
if (f!=null) {
f.setTitle(
APP_TITLE +
" :: " +
fileSystemView.getSystemDisplayName(file) );
}
gui.repaint();
}
public static boolean copyFile(File from, File to) throws IOException {
boolean created = to.createNewFile();
if (created) {
FileChannel fromChannel = null;
FileChannel toChannel = null;
try {
fromChannel = new FileInputStream(from).getChannel();
toChannel = new FileOutputStream(to).getChannel();
toChannel.transferFrom(fromChannel, 0, fromChannel.size());
// set the flags of the to the same as the from
to.setReadable(from.canRead());
to.setWritable(from.canWrite());
to.setExecutable(from.canExecute());
} finally {
if (fromChannel != null) {
fromChannel.close();
}
if (toChannel != null) {
toChannel.close();
}
return false;
}
}
return created;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
// Significantly improves the look of the output in
// terms of the file names returned by FileSystemView!
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception weTried) {
}
JFrame f = new JFrame(APP_TITLE);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FileManager fileManager = new FileManager();
f.setContentPane(fileManager.getGui());
try {
URL urlBig = fileManager.getClass().getResource("fm-icon-32x32.png");
URL urlSmall = fileManager.getClass().getResource("fm-icon-16x16.png");
ArrayList<Image> images = new ArrayList<Image>();
images.add( ImageIO.read(urlBig) );
images.add( ImageIO.read(urlSmall) );
f.setIconImages(images);
} catch(Exception weTried) {}
f.pack();
f.setLocationByPlatform(true);
f.setMinimumSize(f.getSize());
f.setVisible(true);
fileManager.showRootFile();
}
});
}
}
/** A TableModel to hold File[]. */
class FileTableModel extends AbstractTableModel {
private File[] files;
private FileSystemView fileSystemView = FileSystemView.getFileSystemView();
private String[] columns = {
"Icon",
"File",
"Path/name",
"Size",
"Last Modified",
"R",
"W",
"E",
"D",
"F",
};
FileTableModel() {
this(new File[0]);
}
FileTableModel(File[] files) {
this.files = files;
}
public Object getValueAt(int row, int column) {
File file = files[row];
switch (column) {
case 0:
return fileSystemView.getSystemIcon(file);
case 1:
return fileSystemView.getSystemDisplayName(file);
case 2:
return file.getPath();
case 3:
return file.length();
case 4:
return file.lastModified();
case 5:
return file.canRead();
case 6:
return file.canWrite();
case 7:
return file.canExecute();
case 8:
return file.isDirectory();
case 9:
return file.isFile();
default:
System.err.println("Logic Error");
}
return "";
}
public int getColumnCount() {
return columns.length;
}
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return ImageIcon.class;
case 3:
return Long.class;
case 4:
return Date.class;
case 5:
case 6:
case 7:
case 8:
case 9:
return Boolean.class;
}
return String.class;
}
public String getColumnName(int column) {
return columns[column];
}
public int getRowCount() {
return files.length;
}
public File getFile(int row) {
return files[row];
}
public void setFiles(File[] files) {
this.files = files;
fireTableDataChanged();
}
}
/** A TreeCellRenderer for a File. */
class FileTreeCellRenderer extends DefaultTreeCellRenderer {
private FileSystemView fileSystemView;
private JLabel label;
FileTreeCellRenderer() {
label = new JLabel();
label.setOpaque(true);
fileSystemView = FileSystemView.getFileSystemView();
}
@Override
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
File file = (File)node.getUserObject();
label.setIcon(fileSystemView.getSystemIcon(file));
label.setText(fileSystemView.getSystemDisplayName(file));
label.setToolTipText(file.getPath());
if (selected) {
label.setBackground(backgroundSelectionColor);
label.setForeground(textSelectionColor);
} else {
label.setBackground(backgroundNonSelectionColor);
label.setForeground(textNonSelectionColor);
}
return label;
}
}
附录
最后,我决定只需要文件浏览器就可以了。在管理文件时,只需打开父目录并使用操作系统的内置功能来制作新文件,重命名,复制或删除它们以及更改标志即可。
这不仅消除了许多剩余的错误,而且消除了用户的律师在我的软件要求其删除符号链接时如何删除其客户端LAN上的所有内容时与我联系的可能性。 ;)
评论
\ $ \ begingroup \ $
对网络的访问权限为+1,您做了什么更改:-)并且也睡觉了:-)
\ $ \ endgroup \ $
– mKorbel
2011年6月8日在12:25
#4 楼
安德鲁,看起来不错。我使用XP和JDK6.07,在浏览或使用“新建/删除/重命名”功能时没有任何问题。几点评论:
图标标题和所有复选框标题均被截断并显示“ ...”。以下内容为我修复了该问题:
// width = (int)preferred.getWidth()+10;
width = (int)preferred.getWidth()+14;
我发现读取所选节点的文本很困难。它是深蓝色背景上的黑色文本。使用深蓝色背景上的白色文本可以更轻松地读取JTable。我看到默认的树渲染器还支持文本选择/非选择颜色。
如果您真的想让用户为自己的价格感到鼓舞,那么也许在删除目录时,可以提示用户查看他们是否想要首先删除所有文件,然后再删除目录。
#5 楼
有一个错误:对表进行排序时,选择模型行索引和表模型行索引之间的映射被破坏,并且按钮最终在其他文件上运行。 :int row = table.getSelectionModel().getLeadSelectionIndex();
RowSorter sorter = table.getRowSorter();
if ( sorter != null ) {
row = sorter.convertRowIndexToModel( row );
}
#6 楼
它可以在WinXP和Win2008_64b(JRE64b)上运行,并且一切看起来正确。要进行更深入的测试,只需单击每个可访问的Component
,而不会出现任何错误。 ProgresBar可以正常工作,并且只需要很短的时间就可以正确显示网络树的内容。我对Active Directory守护程序(仅次于AD)的假设是,我具有Intranet的本地管理员访问权限,并且看起来像从最终用户PC由本地管理员帐户AD某些人限制了对网络树的视图,AD某些人管理了此访问,但我无法访问AD设置,因为这是一项外包服务。
通过本地管理员帐户在此处登录的WinXP SP3 SK LanguagePack(不是完整的本地化版本,例如西班牙,德国等),单击JTree中的“网络”图标已死,没有出现任何错误,正如我两次描述的那样。
Win7 64b SK(很抱歉,没有时间进行更深入的分析,仅通过本地管理员帐户登录此处,使您的孩子无法使用consol e)JTree中的图标向JTable返回null并通过打开JButton单击并仅返回Win7错误。
Win2008是由AD可访问上下文管理的访问无法创建本地帐户(内部规则)。
评论
到目前为止,我还无法重现您的错误。通常有什么措施可以引发问题吗?@Andrew:我看到您已经为这个问题添加了赏金。这是否意味着您仍然遇到相同的错误,即更改SwingWorker并不能解决问题?否则,更新后的代码仍然存在哪些问题? / Pete
@HFOE:您对SwingWorker所做的更改已解决了该问题。我正试图获得更多的测试。 (找出接下来的7个错误。)
@HFOE:我刚才只是在考虑此线程的问题。该代码太大了,无法进行实质性的补充,并且仍然发布在SO上。我也在考虑也许将此代码作为对SourceForge的首次尝试。是的,那是很大的可能性。话虽如此,我将让它一直运行到赏金期快要结束时,看看是否有新的和杰出的观点出现。不过,(低语)在悬赏金发布之前,任何人都很难超越所提供的建议。 ;)
@Andrew:我想知道您是否处于一个阶段,可以通过在多个程序包中创建可能包含多个类的项目并将其发布到SourceForge上,以使所有人有所作为而得到更好的服务。我并不是说当前存在的问题不值得解决,而仅仅是这些问题的复杂性超出了堆栈溢出最能解决的问题。这将是一个所有人都可以有组织地作出贡献的精巧项目。抱歉,我对此进行了编辑,以添加版本控制,方法是在添加您的评论之前删除我的原始评论,然后在之后添加我的评论。