Results 1 to 4 of 4
  1. #1
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default how to enable copy from Jtree lines

    I use Jtree - I want to know I can I enable the option for user - to copy the content of one line , so he can paste it other gui component (jtextField) or other windows.

    Advanced question is to custom copy - i.e. if the tree is file system, node is file name and its root is the file directory - i want that when you copy the file name it will infact copy the entire path .

    I have already google it , and find other that ask for this - without answer

    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.datatransfer.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    
    public class PathCopy {
        private JScrollPane getTreeComponent() {
            JTree tree = new JTree();
            tree.getSelectionModel().setSelectionMode(
                TreeSelectionModel.SINGLE_TREE_SELECTION);
            tree.setTransferHandler(new CopyHandler());
            tree.addMouseListener(copier);
            expandTree(tree);
            return new JScrollPane(tree);
        }
    
        private void expandTree(JTree tree) {
            DefaultMutableTreeNode root =
                (DefaultMutableTreeNode)tree.getModel().getRoot();
            java.util.Enumeration e = root.breadthFirstEnumeration();
            while(e.hasMoreElements()) {
                DefaultMutableTreeNode node =
                    (DefaultMutableTreeNode)e.nextElement();
                if(node.isLeaf()) continue;
                int row = tree.getRowForPath(new TreePath(node.getPath()));
                tree.expandRow(row);
            }
        }
    
        public static void main(String[] args) {
            PathCopy test = new PathCopy();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getTreeComponent());
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
        }
    
        private MouseListener copier = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if(SwingUtilities.isRightMouseButton(e)) {
                    JTree tree = (JTree)e.getComponent();
                    Clipboard clipboard =
                        Toolkit.getDefaultToolkit().getSystemClipboard();
                    tree.getTransferHandler().exportToClipboard(tree,
                        clipboard, TransferHandler.COPY);
                }
            }
        };
    }
    
    class CopyHandler extends TransferHandler {
        public void exportToClipboard(JComponent comp, Clipboard clip, int action) {
            JTree tree = (JTree)comp;
            TreePath path = tree.getSelectionPath();
            if(path != null) {
                clip.setContents(new StringSelection(path.toString()), null);
            }
        }
    }

  3. #3
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    Its not working

    I tried to debug it - I'm not quite sure what the problem
    I think it there is someting wrong in the copier - it get the tree but the tree dont transfer anything to the copyHandler

    Java Code:
    private MouseListener copier = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if(SwingUtilities.isRightMouseButton(e)) {
                   [COLOR="Red"] JTree tree = (JTree)e.getComponent();[/COLOR]                Clipboard clipboard =
                        Toolkit.getDefaultToolkit().getSystemClipboard();
                    tree.getTransferHandler().exportToClipboard(tree,
                        clipboard, TransferHandler.COPY);
                }
            }
        };

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    You need to create a custom transfer handler and transferable. The default transfer handler has no idea what is behind the JTree.

Similar Threads

  1. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  2. How to enable debug mode in eclipse
    By msgbharath in forum New To Java
    Replies: 3
    Last Post: 01-10-2009, 02:20 AM
  3. How to enable tasklist-view?
    By fredand44 in forum Eclipse
    Replies: 1
    Last Post: 12-18-2008, 09:59 AM
  4. Enable save/print in PPT using OleClientSite
    By marshallbenfield in forum SWT / JFace
    Replies: 1
    Last Post: 12-13-2008, 04:33 AM
  5. Active Content enable problem
    By kasipandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-01-2008, 02:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •