Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-19-2009, 12:46 PM
Member
 
Join Date: Jun 2008
Location: Australia
Posts: 43
Rep Power: 0
javanewbie is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-22-2009, 03:39 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Time to get creative.
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);
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing Information in a node Tenn New To Java 4 04-30-2009 06:43 AM
Node selection in jtree Preethi AWT / Swing 4 06-20-2008 12:25 AM
Display success message in same page when submit button is clicked am17mu JavaServer Pages (JSP) and JSTL 1 03-29-2008 11:56 AM
JTree Programmatic Node Expansion and Selection Probelm hemanthjava AWT / Swing 1 01-17-2008 07:36 AM


All times are GMT +2. The time now is 05:57 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org