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

Tree Cell Renderer

Tree Cell 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.Component;
import java.awt.GridLayout;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

public class BookTree {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Book Tree");
    Book javaBooks[] = {
        new Book("Core Java 2 Fundamentals", "Cornell/Horstmann",
        new Book("Taming Java Threads", "Holub", 34.95f),
        new Book("JavaServer  Pages", "Pekowsky", 39.95f) };
    Book htmlBooks[] = { new Book("Dynamic HTML", "Goodman", 39.95f),
        new Book("HTML 4 Bible", "Pfaffenberger/Gutzman", 49.99f) };
    Vector javaVector = new NamedVector("Java Books", javaBooks);
    Vector htmlVector = new NamedVector("HTML Books", htmlBooks);
    Object rootNodes[] = { javaVector, htmlVector };
    Vector rootVector = new NamedVector("Root", rootNodes);
    JTree tree = new JTree(rootVector);
    TreeCellRenderer renderer = new BookCellRenderer();
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 300);

class Book {
  String title;

  String authors;

  float price;

  public Book(String title, String authors, float price) {
    this.title = title;
    this.authors = authors;
    this.price = price;

  public String getTitle() {
    return title;

  public String getAuthors() {
    return authors;

  public float getPrice() {
    return price;

class BookCellRenderer implements TreeCellRenderer {
  JLabel titleLabel;

  JLabel authorsLabel;

  JLabel priceLabel;

  JPanel renderer;

  DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();

  Color backgroundSelectionColor;

  Color backgroundNonSelectionColor;

  public BookCellRenderer() {
    renderer = new JPanel(new GridLayout(0, 1));
    titleLabel = new JLabel(" ");
    authorsLabel = new JLabel(" ");
    priceLabel = new JLabel(" ");
    backgroundSelectionColor = defaultRenderer
    backgroundNonSelectionColor = defaultRenderer

  public Component getTreeCellRendererComponent(JTree tree, Object value,
      boolean selected, boolean expanded, boolean leaf, int row,
      boolean hasFocus) {
    Component returnValue = null;
    if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
      Object userObject = ((DefaultMutableTreeNode) value)
      if (userObject instanceof Book) {
        Book book = (Book) userObject;
        priceLabel.setText("" + book.getPrice());
        if (selected) {
        } else {
        returnValue = renderer;
    if (returnValue == null) {
      returnValue = defaultRenderer.getTreeCellRendererComponent(tree,
          value, selected, expanded, leaf, row, hasFocus);
    return returnValue;

class NamedVector extends Vector {
  String name;

  public NamedVector(String name) { = name;

  public NamedVector(String name, Object elements[]) { = name;
    for (int i = 0, n = elements.length; i < n; i++) {

  public String toString() {
    return "[" + name + "]";


Related examples in the same category

1.CheckBox Node Tree SampleCheckBox Node Tree Sample
2.Combobox Tree Node EditorCombobox Tree Node Editor
3.extends DefaultTreeCellEditor to create Tree Leaf editorextends DefaultTreeCellEditor to create Tree Leaf editor
4.Tree Changed RendererTree Changed Renderer
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.