Java Utililty Methods JTree

List of utility methods to do JTree

Description

The list of methods to do JTree are organized into topic(s).

Method

voidapplyDefaultProperties(final JTree comp)
Sets default background and foreground color as well as a default font for the specified component.
if (comp == null) {
    return;
applyProperties(comp, "Tree.background", 
        "Tree.foreground", 
        "Tree.font"); 
voidautoscroll(JTree tree, Point cursorLocation)
autoscroll
Insets insets = DEFAULT_INSETS;
Rectangle outer = tree.getVisibleRect();
Rectangle inner = new Rectangle(outer.x + insets.left, outer.y + insets.top,
        outer.width - (insets.left + insets.right), outer.height - (insets.top + insets.bottom));
if (!inner.contains(cursorLocation)) {
    Rectangle scrollRect = new Rectangle(cursorLocation.x - insets.left, cursorLocation.y - insets.top,
            insets.left + insets.right, insets.top + insets.bottom);
    tree.scrollRectToVisible(scrollRect);
...
booleanautoScrollIsDesirable(JTree tree)

Determines whether a tree should be in auto scroll mode.

boolean ret = false;
if (tree.getRowCount() < 1) {
    return ret;
Rectangle viewRect = tree.getVisibleRect();
int[] selected = tree.getSelectionRows();
boolean selectedIsVisible = false;
if (selected == null) {
...
voidclearTreeIcon(JTree tree)
clear Tree Icon
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
renderer.setOpenIcon(null);
renderer.setClosedIcon(null);
tree.setCellRenderer(renderer);
voidcorrectRowHeight(JTree tree)
Update tree row height to the best possible considering font size changes on the current platform http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4760081
FontMetrics metrics = tree.getFontMetrics(tree.getFont());
tree.setRowHeight(Math.max(tree.getRowHeight(), metrics.getHeight()));
voiddisposeExpressionsTree()
dispose Expressions Tree
frame.dispose();
voidenableAutoExpansion(final JTree tree)
Adds a TreeModelListener that will automatically expand the tree as new nodes are inserted.
tree.getModel().addTreeModelListener(new TreeModelListener() {
    public void treeNodesChanged(TreeModelEvent e) {
    public void treeNodesInserted(TreeModelEvent e) {
        tree.expandPath(e.getTreePath());
    public void treeNodesRemoved(TreeModelEvent e) {
    public void treeStructureChanged(TreeModelEvent e) {
});
TreePathfindByName(JTree tree, String[] names)
find By Name
TreeNode root = (TreeNode) tree.getModel().getRoot();
return find2(tree, new TreePath(root), names, 0, true);
JTreefindTree(Container container)
Traverse a container hierarchy and returns the first JMenuBar it finds.
Component[] components = container.getComponents();
for (Component component : components) {
    if (component instanceof JTree) {
        return (JTree) component;
    } else if (component instanceof Container) {
        JTree tree = findTree((Container) component);
        if (tree != null) {
            return tree;
...
intgetCurrentWidth(JTree tree, int row)
get Current Width
return tree.getSize().width - 100;