JTree: convertValueToText(Object v,boolean s,boolean e,boolean l,int r,boolean h) : JTree « javax.swing « Java by API






JTree: convertValueToText(Object v,boolean s,boolean e,boolean l,int r,boolean h)

  
import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

public class MainClass {

  public static void main(final String args[]) {
    JFrame frame = new JFrame("Tree Tips");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTree tree = new JTree(new String[]{"a","b","c"});
    ToolTipManager.sharedInstance().registerComponent(tree);
    TreeCellRenderer renderer = new ToolTipTreeCellRenderer();
    tree.setCellRenderer(renderer);
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}

class ToolTipTreeCellRenderer implements TreeCellRenderer {
  DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();

  public ToolTipTreeCellRenderer() {
  }

  public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
      boolean expanded, boolean leaf, int row, boolean hasFocus) {

    Object tip = null;
    if (value != null) {
      
      if (value instanceof DefaultMutableTreeNode) {
        tip = ((DefaultMutableTreeNode) value).getUserObject();
      } else {
        tip = tree.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
      }
      renderer.setToolTipText((String)tip);
    }
    renderer.setText((String)tip);
    return renderer;
  }
}
           
         
    
  








Related examples in the same category

1.Tree.closedIcon
2.Tree.collapsedIcon
3.Tree.expandedIcon
4.Tree.leafIcon
5.Tree.openIcon
6.JTree.DynamicUtilTreeNode: createChildren(DefaultMutableTreeNode parent, Object children)
7.new JTree()
8.new JTree(TreeNode root)
9.new JTree(Vector vector)
10.JTree: addTreeSelectionListener(TreeSelectionListener tsl)
11.JTree: addTreeWillExpandListener(TreeWillExpandListener tel)
12.JTree: getCellRenderer()
13.JTree: getLeadSelectionRow()
14.JTree: getMaxSelectionRow()
15.JTree: getMinSelectionRow()
16.JTree: getNextMatch(String prefix, int startingRow, Bias bias)
17.JTree: getPathForLocation(int x, int y)
18.JTree: getRowHeight()
19.JTree: getSelectionRows()
20.JTree: putClientProperty('JTree.lineStyle', 'None')
21.JTree: setAutoscrolls(boolean b)
22.JTree: setCellEditor(TreeCellEditor editor)
23.JTree: setExpandedState(TreePath path, boolean state)
24.JTree: setRowHeight(int rowHeight)
25.JTree: setTransferHandler(TransferHandler newHandler) (Drag and drop files from file explore)