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

voidscrollTreeToSelectedRow(final JTree tree)
scroll Tree To Selected Row
final int selectedRow = tree.getLeadSelectionRow();
if (selectedRow >= 0) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            tree.scrollRectToVisible(tree.getRowBounds(selectedRow));
    });
voidsetComponentTreeEnabled(Component c, boolean enabled)
Enables or disables of the components in the tree starting with c .
c.setEnabled(enabled);
Component[] children = getChildren(c);
if (children != null) {
    for (int i = 0; i < children.length; i++) {
        setComponentTreeEnabled(children[i], enabled);
voidsetLeftChildIndent(JTree tree, int indent)
Set's the left indent in pixels to use for the given JTree 's collapsed and expanded icon.
((BasicTreeUI) tree.getUI()).setLeftChildIndent(indent);
voidsetToolTipOnTree(Container c, String tooltip)
Recurse the Component hierarchy, setting the tooltip of each component.
for (int i = 0; i < c.getComponentCount(); i++) {
    Component child = c.getComponent(i);
    if (child instanceof Container) {
        setToolTipOnTree((Container) child, tooltip);
    if (child instanceof JComponent) {
        ((JComponent) child).setToolTipText(tooltip);
voidsetTreeEnabled(Container container, boolean enabled)
set Tree Enabled
synchronized (container.getTreeLock()) {
    for (Component comp : container.getComponents()) {
        setComponentEnabled(comp, enabled);
TreeSelectionListenertreeListen(final ActionListener listener)
Wrapper for tree selection listeners.
return new TreeSelectionListener() {
    public void valueChanged(TreeSelectionEvent e) {
        listener.actionPerformed(new ActionEvent(e.getSource(), 0, "bogus commandus"));
};
voidupMove(JTree tree)
up Move
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (node == null)
    return;
if (node.isRoot())
    return;
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
int index = parent.getIndex(node);
if (index <= 0)
...