我想将一个文件夹内的文件以图标的形式显示在窗口内,单击选择,双击打开,像JFileChooser那样,不过JFileChooser是弹出窗口,嵌在窗口内又不好看能够自己做吗,是不是可以将JFileChooser的周边按钮去掉
解决方案 »
- 有关 jxl的问题,求高手解答
- swing如何实现onmouseover不规则图片
- 关于ConcurrentHashMap, thread-safe的问题
- 请问楼主结贴了意味着什么?得到分或给人家分又意味着什么?
- 大家帮忙看看这个怎么输出。
- 请问如何让JTable不能拖放交换列的位置?
- 有哪位高手肯帮小弟一个忙吗?我碰到了一个大难题!关于数组方面的!
- 请问java applet里,怎么能只显示图像的一部分,比如只显示图片的中间一块?
- 送分: 一个对C和C++很熟练的程序员要学JAVA 一般需多长时间?
- 不会使用API,只会查,但是看不明白方法说明,不会使用。
- 删除树组中的某个值
- Japplet
public class TreeNode extends JFrame {
public TreeNode() {
final JTree tree = new JTree(createTreeModel());
JScrollPane scrollPane = new JScrollPane(tree); getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(GJApp.getStatusArea(), BorderLayout.SOUTH); tree.addTreeExpansionListener(new TreeExpansionListener() {
public void treeCollapsed(TreeExpansionEvent e) {
} public void treeExpanded(TreeExpansionEvent e) {
TreePath path = e.getPath();
FileNode node = (FileNode) path.getLastPathComponent(); if (!node.isExplored()) {
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
GJApp.updateStatus("exploring ..."); UpdateStatus us = new UpdateStatus();
us.start(); node.explore();
model.nodeStructureChanged(node);
}
} class UpdateStatus extends Thread {
public void run() {
try {
Thread.currentThread().sleep(450);
} catch (InterruptedException e) {
} SwingUtilities.invokeLater(new Runnable() {
public void run() {
GJApp.updateStatus(" ");
}
});
}
}
});
} private DefaultTreeModel createTreeModel() {
File root = new File("E:/");
FileNode rootNode = new FileNode(root); rootNode.explore();
return new DefaultTreeModel(rootNode);
} public static void main(String args[]) {
GJApp.launch(new TreeNode(), "JTree File Explorer", 300, 300, 450, 400);
}
}class FileNode extends DefaultMutableTreeNode {
private boolean explored = false; public FileNode(File file) {
setUserObject(file);
} public boolean getAllowsChildren() {
return isDirectory();
} public boolean isLeaf() {
return !isDirectory();
} public File getFile() {
return (File) getUserObject();
} public boolean isExplored() {
return explored;
} public boolean isDirectory() {
File file = getFile();
return file.isDirectory();
} public String toString() {
File file = (File) getUserObject();
String filename = file.toString();
int index = filename.lastIndexOf(File.separator); return (index != -1 && index != filename.length() - 1) ? filename
.substring(index + 1) : filename;
} public void explore() {
if (!isDirectory())
return; if (!isExplored()) {
File file = getFile();
File[] children = file.listFiles(); for (int i = 0; i < children.length; ++i)
add(new FileNode(children[i])); explored = true;
}
}
}class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel(); static private JLabel status = new JLabel(" "); public static void launch(final JFrame f, String title, final int x,
final int y, final int w, int h) {
f.setTitle(title);
f.setBounds(x, y, w, h);
f.setVisible(true); statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT); f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
} static public JPanel getStatusArea() {
return statusArea;
} static public void updateStatus(String s) {
status.setText(s);
}
}
public class GraphSelectionDemo extends DefaultTreeModel implements
GraphModelListener { private static JTree tree; private static JGraph graph; private static GraphSelectionDemo gtModel; private static GraphModelTreeNode gtModelTreeNode;
private static SyncGraphSelectionListener mySyncTreeSelectionListener = new SyncGraphSelectionListener(); public static void main(String[] args) {
JFrame frame = new JFrame("GraphSelectionDemo");
graph = new JGraph();
gtModel = new GraphSelectionDemo(graph.getModel());
gtModelTreeNode = new GraphModelTreeNode(graph.getModel());
graph.getModel().addGraphModelListener(gtModel);
tree = new JTree(gtModel);
tree.setRootVisible(false);
JScrollPane sGraph = new JScrollPane(graph);
JScrollPane sTree = new JScrollPane(tree);
tree.addTreeSelectionListener(new SyncTreeSelectionListener());
graph.addGraphSelectionListener(mySyncTreeSelectionListener);
graph.addMouseListener(new MyMouseListener());
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sTree,
sGraph);
frame.getContentPane().add(pane);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} public GraphSelectionDemo(GraphModel model) {
super(new GraphModelTreeNode(model));
} public void graphChanged(GraphModelEvent e) {
reload();
} public static class SyncTreeSelectionListener implements
TreeSelectionListener { public void valueChanged(TreeSelectionEvent e) {
if (tree.isSelectionEmpty())//a selection in the graph can trigger that event while no tree selection exists yet
return;
TreePath[] treeSelection = tree.getSelectionModel()
.getSelectionPaths();
Object[] graphSelection = new Object[treeSelection.length]; for (int i = 0; i < treeSelection.length; i++) {
graphSelection[i] = treeSelection[i].getLastPathComponent();
} // now we set the corresponding graph selection
graph.setSelectionCells(graphSelection); /*
* System.out .print(tree.getSelectionModel().getSelectionPath() +
* "\n");
*/
}
}
/**
* Prevent from loosing the synchronisation after a graph element is dragged
*/
public static class MyMouseListener extends MouseInputAdapter {
public void mouseReleased(MouseEvent e) {
if (e.getSource() instanceof JGraph) {
mySyncTreeSelectionListener.valueChanged(null);
}
}
} public static class SyncGraphSelectionListener implements
GraphSelectionListener {
public void valueChanged(GraphSelectionEvent e) {
Object selectedPort = null;
Object[] selection = graph.getSelectionModel().getSelectionCells();
if (selection == null)
return;
TreePath[] paths = new TreePath[selection.length];
for (int i = 0; i < selection.length; i++) { ArrayList list = new ArrayList();
Object selected = selection[i];
if (selected == null)
return;
list.add(tree.getModel().getRoot());
if (graph.getModel().isPort(selected)) {// shift if a port
// is
// selected
selectedPort = selected;
selected = graph.getModel().getParent(selected);
} list = computeTreePathSelection(list, graph.getModel()
.getParent(selected), selected);
if (selectedPort != null)
list.add(selectedPort);
TreePath treePath = new TreePath(list.toArray()); if (treePath != null) {
paths[i] = treePath;
// System.out.print( treePath.toString() + "\n");
} } // to emphasis the change, we collapse the whole tree before
// assigning a new selection
int treeSize = tree.getRowCount();
int row = 0;
while (row < treeSize) {
tree.collapseRow(row);
row++;
} // now we set the corresponding tree selection
tree.setSelectionPaths(paths);
} /**
* Recursively adds the selected graph cells to the array list, starting
* from the current child and going until the graph root.
*
* @param currentList
* @param parent
* @param child
* @return collection of selected graph cells
*/
public ArrayList computeTreePathSelection(ArrayList currentList,
Object parent, Object child) {
if (parent == null) {
currentList.add(child);
return currentList;
} int parentIndex = graph.getModel().getIndexOfRoot(parent); if (parentIndex < 0) {// then parent is in a group or is a port
computeTreePathSelection(currentList, graph.getModel()
.getParent(parent), parent);
} // parentIndex refer to a valid selected node in the tree
currentList.add(gtModelTreeNode.getChildAt(parentIndex)); // index of the child in the graph
int childIndex = graph.getModel().getIndexOfChild(parent, child);
// corresponding node in the tree
Object node = tree.getModel().getChild(parent, childIndex); currentList.add(node);
return currentList;
}
} /**
* See the GraphModelTree demo
*/
public static class GraphModelTreeNode implements TreeNode { protected GraphModel model; public GraphModelTreeNode(GraphModel model) {
this.model = model;
} public Enumeration children() {
Vector v = new Vector();
for (int i = 0; i < model.getRootCount(); i++)
v.add(model.getRootAt(i));
return v.elements();
} public boolean getAllowsChildren() {
return true;
} public TreeNode getChildAt(int childIndex) {
return (TreeNode) model.getRootAt(childIndex);
} public int getChildCount() {
return model.getRootCount();
} public int getIndex(TreeNode node) {
return model.getIndexOfRoot(node);
} public TreeNode getParent() {
return null;
} public boolean isLeaf() {
return false;
} public String toString() {
return model.toString();
}
}}