Tree Changed Renderer : Tree Renderer Editor « Swing JFC « Java






Tree Changed Renderer

Tree Changed Renderer
  
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

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 TreeChangedRenderer {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Changed Renderer");
    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);
    }

    // Swap background colors
    Color backgroundSelection = renderer.getBackgroundSelectionColor();
    renderer.setBackgroundSelectionColor(renderer
        .getBackgroundNonSelectionColor());
    renderer.setBackgroundNonSelectionColor(backgroundSelection);

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

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

           
         
    
  








Related examples in the same category

1.Tree Cell RendererTree Cell Renderer
2.CheckBox Node Tree SampleCheckBox Node Tree Sample
3.Combobox Tree Node EditorCombobox Tree Node Editor
4.extends DefaultTreeCellEditor to create Tree Leaf editorextends DefaultTreeCellEditor to create Tree Leaf editor
5.Build a tree and populate it with custom renderers and editorsBuild a tree and populate it with custom renderers and editors
6.Using DefaultTreeCellRenderer
7.Scrollable Table Cell Renderer
8.This program demonstrates cell rendering and listening to tree selection events.This program demonstrates cell rendering and listening to tree selection events.