开发者

java swing : display file system in jtree, and how to open the file

I am using jsplit pane, in left component , there is tree view that display folder and file of home directory. When i click on the leaf node of a tree , if it is not a directory, then file (image, pdf, text, video, etc..) should open开发者_运维问答 in right component of jsplit pane.

how can i display that file right component of jsplit pane when we select file in jtree in left pane.

Please provide code snippets Its urgent. Thanks


Like this? http://www.java2s.com/Code/Java/Swing-JFC/DisplayafilesysteminaJTreeview.htm


This not possible. There is no way to be able "display" the file on the right side. You can however display some information about the file and/or define certain formats that you will be able to display (images, text, etc.)


First, create the JTree. For the next step, you need to override the valueChanged method as shown below. Note that your class needs to implement TreeSelectionListener to look for the TreeSelectionEvent.

//tree selction event on the tree triggers this method
public void valueChanged(TreeSelectionEvent e) {
        //which node was selected
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
        if (node == null) return;
        Object nodeInfo = node.getUserObject();

        //if node is a leaf
        if (node.isLeaf()) {
        //do something with the above object

Perhaps you can run a toString on the nodeInfo object and use this string to retrieve details about that file from the database and respectively display its details on the right side of the JSplitPane.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜