DefaultTreeCellRenderer: setBackgroundSelectionColor(Color newColor) : DefaultTreeCellRenderer « javax.swing.tree « Java by API






DefaultTreeCellRenderer: setBackgroundSelectionColor(Color newColor)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

public class MainClass {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Changed Renderer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTree tree = new JTree();
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();

    renderer.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 32));
    int rowHeight = tree.getRowHeight();
    if (rowHeight <= 0) {
      tree.setRowHeight(rowHeight - 1);
    }

    Color backgroundSelection = renderer.getBackgroundSelectionColor();
    renderer.setBackgroundSelectionColor(Color.blue);
    renderer.setBackgroundNonSelectionColor(backgroundSelection);

    // Swap text colors
    Color textSelection = renderer.getTextSelectionColor();
    renderer.setTextSelectionColor(Color.green);
    renderer.setTextNonSelectionColor(textSelection);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}

           
       








Related examples in the same category

1.DefaultTreeCellRenderer: getBackgroundSelectionColor()
2.DefaultTreeCellRenderer: getTextSelectionColor()
3.DefaultTreeCellRenderer: setBackgroundNonSelectionColor(Color newColor)
4.DefaultTreeCellRenderer: setClosedIcon(Icon newIcon)
5.DefaultTreeCellRenderer: setLeafIcon(Icon newIcon)
6.DefaultTreeCellRenderer: setOpenIcon(Icon newIcon)
7.DefaultTreeCellRenderer: setTextNonSelectionColor(Color newColor)
8.DefaultTreeCellRenderer: setTextSelectionColor(Color newColor)