JXTreeTable and double click

View: New views
4 Messages — Rating Filter:   Alert me  

JXTreeTable and double click

by jdnc-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

i've got a JXTreeTable and would like to assume the value take over with a double click.
I added a MouseListener on the table
[code]
            treeTable.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 2) {
    System.out.println("2");
    }

    }
            });
[/code]
but the problem is that the Mouselistener is reacting at +/- which shouldn't happen. it should only react if i double click on a leaf or diretory.
In a JTree that is no Problem, there is the method
selectedRow = jTree.getRowForLocation(me.getX(), me.getY());

Has anyone an idea?

Thanks...

Message was edited by: sirwayne
[Message sent by forum member 'sirwayne' (fremdland@...)]

http://forums.java.net/jive/thread.jspa?messageID=369091

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@...
For additional commands, e-mail: jdnc-help@...


Re: JXTreeTable and double click

by jdnc-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

don't quite understand what exactly you are trying to achieve. So it's time the usual small (!) standalone (!) runnable (!)  ... blabla :-) Best show JTree side-by-side with a JXTreeTable so we can easily compare the different behaviour ( which version of jdk/swingx?)

CU
Jeanette
[Message sent by forum member 'kleopatra' (fastegal@...)]

http://forums.java.net/jive/thread.jspa?messageID=369185

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@...
For additional commands, e-mail: jdnc-help@...


Re: JXTreeTable and double click

by jdnc-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

[code]
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;

import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;

public class TestTree extends JFrame{
       
        public TestTree()
        {
                super("TestTree");
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               
                DefaultMutableTreeNode top = new DefaultMutableTreeNode(
                                "The Java Series");
                createNodes(top);
                final JTree tree = new JTree(top);
               
                tree.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                                int selectedRow = tree.getRowForLocation(e.getX(), e.getY());
                                if (selectedRow != -1) {
                                        if (e.getClickCount() == 2) {
                                                System.out.println("2");
                                        }
                                }

                        }
                });
                JScrollPane treeView = new JScrollPane(tree);
                add(treeView,BorderLayout.EAST);
                final TreeModel model = new TreeModel();
                final JXTreeTable jxTreeTable = new JXTreeTable(model);
                jxTreeTable.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                                int selectedRow = jxTreeTable.getSelectedRow();
                                if (selectedRow != -1) {
                                        if (e.getClickCount() == 2) {
                                                System.out.println("2");
                                        }
                                }

                        }
                });
               
                add(jxTreeTable,BorderLayout.CENTER);
               
                pack();
        }

        public static void main(String[] args) {
                try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (InstantiationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (UnsupportedLookAndFeelException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                SwingUtilities.invokeLater(new Runnable() {
                       
                        @Override
                        public void run() {
                                new TestTree().setVisible(true);
                               
                        }
                });

        }

        private static void createNodes(DefaultMutableTreeNode top) {
                DefaultMutableTreeNode category = null;
                DefaultMutableTreeNode book = null;

                category = new DefaultMutableTreeNode("Books for Java Programmers");
                top.add(category);

                // original Tutorial
                book = new DefaultMutableTreeNode(
                                "The Java Tutorial: A Short Course on the Basics");
                category.add(book);

                // Tutorial Continued
                book = new DefaultMutableTreeNode(
                                "The Java Tutorial Continued: The Rest of the JDK");
                category.add(book);

                // JFC Swing Tutorial
                book = new DefaultMutableTreeNode(
                                "The JFC Swing Tutorial: A Guide to Constructing GUIs");
                category.add(book);
        }
       
        public class TreeModel extends AbstractTreeTableModel {

            private TreeNode root;
                public TreeModel() {
                        super();
                       
                        root = new TreeNode("Parent");
                        buildTree();
                }
               
                @Override
                public TreeNode getRoot() {
                        return root;
                }

                @Override
                public int getColumnCount() {
                        return 1;
                }

                @Override
                public boolean isCellEditable(Object node, int column) {
                        return false;
                }

                @Override
                public String getColumnName(int i) {
                        switch (i) {
                        case 0:
                                return "Name";
                        default:
                                return "Default";
                        }
                }

                @Override
                public Object getValueAt(Object node, int col) {      
                        TreeNode treeNode = (TreeNode) node;
                        switch (col) {
                case 0:
                    return treeNode.getName();
                default: return "Default";
                }
                }

                @Override
                public Object getChild(Object parent, int index) {
                         return ((TreeNode) parent).getChildren().get(index);
                }

                @Override
                public int getChildCount(Object parent) {
                        return ((TreeNode) parent).getChildren().size();
                }

                @Override
                public int getIndexOfChild(Object parent, Object child) {
            if(child == null) {
            return -1;
            } else {
            TreeNode treeNode = (TreeNode) parent;
            List<TreeNode> children = treeNode.getChildren();
            if(children.isEmpty()) {
            return 0;
            } else {
            return children.indexOf(child);
            }
            }
                }
               
            @Override
                public boolean isLeaf(Object node) {
                return ((TreeNode) node).isLeaf();
            }

                public void buildTree() {
                       
                        TreeNode top = new TreeNode("Books for Java Programmers");
                       
                        top.addChildrenNode(new TreeNode("The Java Tutorial: A Short Course on the Basics"));
                       
                        top.addChildrenNode( new TreeNode("The Java Tutorial Continued: The Rest of the JDK"));
                       
                        top.addChildrenNode(new TreeNode("The JFC Swing Tutorial: A Guide to Constructing GUIs"));
                       
                        root.addChildrenNode(top);
                        modelSupport.fireNewRoot();
                }
        }
       
        static class TreeNode implements Comparable<TreeNode>
        {
                private String name;
                private TreeNode parent;
                private final TreeSet<TreeNode> children;
               
                public TreeNode(String name){
                        this.name = name;
                        children = new TreeSet<TreeNode>();
                }
               
                public String getName() {
                        return name;
                }

                public void setName(String name) {
                        this.name = name;
                }

               
                public boolean isLeaf() {
                        return children.isEmpty();
                }

                public boolean isRoot() {
                        return false;
                }

                public List<TreeNode> getChildren() {
                        return new ArrayList<TreeNode>(children);
                }
               
                public void addChildrenNode(TreeNode node) {
                        children.add(node);
                }
               
                public void removeChildrenNode(TreeNode node) {
                        children.remove(node);
                }

                @Override
                public int compareTo(TreeNode o) {
                        return name.compareTo(o.getName());
                }

                public void setParent(TreeNode parent) {
                        this.parent = parent;
                }

                public TreeNode getParent() {
                        return parent;
                }

        }


}
[/code]

1. jdk 1.6 update 16 swingx 0.97
2. Don't understand why the parent in the JXTreeTable doesn't appear.
3. if you click in +/- in the JTree the MouseListener doesn't react, in the JXTreeTable it react.
4. The Selection is different too, JTree the folder icon and +/- isn't selected in the JXTreeTable it is selected.

Thanks for help.
[Message sent by forum member 'sirwayne' (fremdland@...)]

http://forums.java.net/jive/thread.jspa?messageID=369289

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@...
For additional commands, e-mail: jdnc-help@...


Re: JXTreeTable and double click

by jdnc-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> 1. jdk 1.6 update 16 swingx 0.97

why not use the final SwingX (no api changes against 0.97 only bug fixes). Plus I would suggest to go to current head - if you are allowed - it's very near to the pending 1.6 release, aligned with jdk6 and has tons of additional bug fixes.

> 2. Don't understand why the parent in the JXTreeTable
> doesn't appear.

different default for isRootVisible

> 3. if you click in +/- in the JTree the MouseListener
> doesn't react, in the JXTreeTable it react.

still does - problem is if the underlying tree doesn't consume the mouseEvent, the table thinks it's hers. Not very inclined to add yet another hacker (over-next version or so most probably will have JXXTreeTable as default treetable component) here - please have a look into the source and play with the treetablehacker <g>

> 4. The Selection is different too, JTree the folder
> icon and +/- isn't selected in the JXTreeTable it is
> selected.

works okay for a single click (no selection), for a double-click, same reason as 3.

Cheers
Jeanette
[Message sent by forum member 'kleopatra' (fastegal@...)]

http://forums.java.net/jive/thread.jspa?messageID=369295

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@...
For additional commands, e-mail: jdnc-help@...