Question

I'm working with Eclipse and I've got a question about JXTreeTables. I want a window, showing some information about the node, to pop up, when a node is double-clicked. Now, is it possible to get the double-clicked node of the JXTreeTable or null if the click wasn't directly on a node?

Was it helpful?

Solution

I've received an answer on the thread kleopatra mentioned which works perfectly fine and is simplier. Here is the code:

treeTable.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(final MouseEvent e) {
        if (e.getClickCount() != 2) {
            return;
        }

        final int rowIndex = treeTable.rowAtPoint(e.getPoint());

        if (rowIndex < 0) {
            return;
        }

        final TreeTableNode selectedNode = (TreeTableNode)treeTable.getPathForRow(rowIndex).getLastPathComponent();
    }
});

OTHER TIPS

Assuming you mean the behaviour of tree.getRowForLocation(...): there is no api on the treeTable, you hit missing api and might consider to file an improvement issue in the swingx issue tracker :-)

Until that'll be available, you have to do it yourself in a custom MouseListener which delegates to the respective tree method. Going slightly (cough ..) dirty in type-casting the renderer for the hierarchical column to a JTree:

    MouseListener l = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() != 2) return;
            int column = treeTable.columnAtPoint(e.getPoint());
            if (!treeTable.isHierarchical(column)) return;
            Rectangle cell = treeTable.getCellRect(0, column, false);
            JXTree tree = (JXTree) treeTable.getCellRenderer(0, column);
            // translate x to tree coordinates
            int translatedX = e.getX() - cell.x;
            int row = tree.getRowForLocation(translatedX, e.getY());
            LOG.info("row " + row);
        }

    };
    treeTable.addMouseListener(l);

Just for the record, there's a parallel thread in the Swinglabs forum over at java.net

Edit

The woes of assumptions ;-)

With the OPs own answer the listener will fire on a double click anywhere in the table cell which contains the node, not just when directly over the node (aka: its text). So turns out the requirement is more along the lines of tree.getClosestRowForLocation(..) than the assumed tree.getRowForLocation(..).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top