Results 1 to 2 of 2
  1. #1
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Question Display tooltip information when a node from JTree is clicked

    Upon clicking a specific node from the JTree, a tooltip will display certain information coming from a specific cell (eg. Column 2, Row 5) displayed in Jtable.


    any suggestions?

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

    Default

    Time to get creative.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.tree.*;
    
    public class TipTest extends MouseAdapter {
        JTree tree;
        JWindow toolTip;
        JLabel tipLabel;
        Rectangle pathBounds;
        JTable table;
    
        public TipTest(JFrame f) {
            toolTip = new JWindow(f);
            toolTip.setBackground(UIManager.getColor("ToolTip.background"));
            tipLabel = new JLabel();
            toolTip.add(tipLabel);
        }
    
        public void mousePressed(MouseEvent e) {
            Point p= e.getPoint();
            TreePath path = tree.getPathForLocation(p.x, p.y);
            if(path == null) return;
            DefaultMutableTreeNode node =
                (DefaultMutableTreeNode)path.getLastPathComponent();
            //System.out.printf("node = %s%n", node);
            int row = table.getSelectedRow();
            int col = table.getSelectedColumn();
            if(row != -1 && col != -1) {
                String cellStr = (String)table.getValueAt(row, col);
                tipLabel.setText(cellStr);
                toolTip.pack();
                SwingUtilities.convertPointToScreen(p, tree);
                toolTip.setLocation(p.x+5, p.y-toolTip.getHeight());
                toolTip.setVisible(true);
                pathBounds = tree.getPathBounds(path);
            }
        }
    
        public void mouseMoved(MouseEvent e) {
            if(toolTip.isVisible()) {
                if(!pathBounds.contains(e.getPoint())) {
                    toolTip.setVisible(false);
                }
            }
        }
    
        private JScrollPane getTreeComponent() {
            tree = new JTree();
            expandTree(tree);
            tree.addMouseListener(this);
            tree.addMouseMotionListener(this);
            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);
            }
        }
    
        private JScrollPane getTableComponent() {
            table = new JTable(getModel());
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.setCellSelectionEnabled(true);
            Dimension d = table.getPreferredSize();
            table.setPreferredScrollableViewportSize(d);
            return new JScrollPane(table);
        }
    
        private AbstractTableModel getModel() {
            return new AbstractTableModel() {
                public int getRowCount() { return 5; }
    
                public int getColumnCount() { return 4; }
    
                public Object getValueAt(int row, int col) {
                    return String.valueOf((row+1)) + (col+1);
                }
            };
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            TipTest test = new TipTest(f);
            f.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            f.add(test.getTreeComponent(), gbc);
            f.add(test.getTableComponent(), gbc);
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

Similar Threads

  1. JTree Programmatic Node Expansion and Selection Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 3
    Last Post: 01-16-2013, 08:23 AM
  2. Printing Information in a node
    By Tenn in forum New To Java
    Replies: 4
    Last Post: 04-30-2009, 06:43 AM
  3. How to add JCheckbox as a node in JTree
    By shajuantony in forum New To Java
    Replies: 0
    Last Post: 04-09-2009, 08:19 AM
  4. Node selection in jtree
    By Preethi in forum AWT / Swing
    Replies: 4
    Last Post: 06-20-2008, 12:25 AM
  5. Display success message in same page when submit button is clicked
    By am17mu in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 11:56 AM

Posting Permissions

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