ToolTip Tree Example : Tree « Swing Components « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Swing Components » TreeScreenshots 
ToolTip Tree Example
ToolTip Tree Example


// Example from http://www.crionics.com/products/opensource/faq/swing_ex/SwingExamples.html

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

/**
 @version 1.0 11/09/98
 */
class ToolTipTreeNode extends DefaultMutableTreeNode {
  private String toolTipText;

  public ToolTipTreeNode(String str, String toolTipText) {
    super(str);
    this.toolTipText = toolTipText;
  }

  public String getToolTipText() {
    return toolTipText;
  }
}

public class ToolTipTreeExample extends JFrame {
  public ToolTipTreeExample() {
    super("ToolTipTreeExample");
    String[][] strs = { { "swing""boat" }// 0
        "platf""paltform" }// 1
        "basic""fortran" }// 2
        "metal""heavy" }// 3
        "JTree""three" } }// 4

    ToolTipTreeNode[] nodes = new ToolTipTreeNode[strs.length];
    for (int i = 0; i < strs.length; i++) {
      nodes[inew ToolTipTreeNode(strs[i][0], strs[i][1]);
    }
    nodes[0].add(nodes[1]);
    nodes[1].add(nodes[2]);
    nodes[1].add(nodes[3]);
    nodes[0].add(nodes[4]);
    JTree tree = new JTree(nodes[0]) {
      public String getToolTipText(MouseEvent evt) {
        if (getRowForLocation(evt.getX(), evt.getY()) == -1)
          return null;
        TreePath curPath = getPathForLocation(evt.getX(), evt.getY());
        return ((ToolTipTreeNodecurPath.getLastPathComponent())
            .getToolTipText();
      }
    };
    tree.setToolTipText("");
    JScrollPane sp = new JScrollPane(tree);
    getContentPane().add(sp, BorderLayout.CENTER);
  }

  public static void main(String args[]) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    catch (Exception evt) {}
  
    ToolTipTreeExample frame = new ToolTipTreeExample();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.setSize(300150);
    frame.setVisible(true);
  }
}

           
       
Related examples in the same category
1.Disabled Node Tree ExampleDisabled Node Tree Example
2.Disabled Node Tree Example 2Disabled Node Tree Example 2
3.Only Text Tree ExampleOnly Text Tree Example
4.Animated Icon Tree ExampleAnimated Icon Tree Example
5.Animated Icon Tree Example 2
6.MultiLine Tree ExampleMultiLine Tree Example
7.Wide Editor Tree ExampleWide Editor Tree Example
8.Checkbox Node Tree ExampleCheckbox Node Tree Example
9.Invisible Node Tree ExampleInvisible Node Tree Example
10.Icon Node Tree ExampleIcon Node Tree Example
11.Source code for building a tree in Swing
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.