Java JTree repaintSelection(JTree tree)

Here you can find the source of repaintSelection(JTree tree)

Description

Repaints the selection.

License

Open Source License

Parameter

Parameter Description
tree the JTree to repaint the selection of.

Declaration

public static void repaintSelection(JTree tree) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Rectangle;

import javax.swing.JTree;

public class Main {
    /**/*w w w  .  ja  v a  2s  .c om*/
     * Repaints the selection. This allows the row selection to have a
     * background color that changes based on the focus state of the
     * component.
     *
     * @param tree the {@code JTree} to repaint the selection of.
     */
    public static void repaintSelection(JTree tree) {
        int[] selectedRows = tree.getSelectionRows();

        // if there is a selected row, then repaint it.
        if (selectedRows != null && selectedRows.length > 0) {
            // grab the bounds of the first and last selected cells in column
            // one (index zero).
            Rectangle firstSelectedCell = tree.getRowBounds(selectedRows[0]);
            Rectangle lastSelectedCell = tree.getRowBounds(selectedRows[selectedRows.length - 1]);

            // create the rectangle to repaint by unioning the first and last
            // selected cells in column one and then extending that rectangle
            // to extend from the left edge of the table all the way to the
            // right edge.
            Rectangle repaintRectangle = firstSelectedCell.union(lastSelectedCell);
            repaintRectangle.x = 0;
            repaintRectangle.width = tree.getWidth();

            // repaint the selection area.
            tree.repaint(repaintRectangle);
        }

    }
}

Related

  1. maxDepth(JTree tree, int depth)
  2. paint(JTree tree)
  3. pickFirstIfNone(JTree tree)
  4. printComponentTree(final JComponent fc)
  5. RemoveAll(JTree tree)
  6. restoreExpanstionState(JTree tree, int row, String expansionState)
  7. revalidateTree(java.awt.Component c)
  8. saveExpansionState(JTree tree)
  9. saveExpansionStateStrings(JTree tree)