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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing JFC » Tree Renderer Editor 




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",
            42.99f),
        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();
    tree.setCellRenderer(renderer);
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300300);
    frame.setVisible(true);
  }
}

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(01));
    titleLabel = new JLabel(" ");
    titleLabel.setForeground(Color.blue);
    renderer.add(titleLabel);
    authorsLabel = new JLabel(" ");
    authorsLabel.setForeground(Color.blue);
    renderer.add(authorsLabel);
    priceLabel = new JLabel(" ");
    priceLabel.setHorizontalAlignment(JLabel.RIGHT);
    priceLabel.setForeground(Color.red);
    renderer.add(priceLabel);
    renderer.setBorder(BorderFactory.createLineBorder(Color.black));
    backgroundSelectionColor = defaultRenderer
        .getBackgroundSelectionColor();
    backgroundNonSelectionColor = defaultRenderer
        .getBackgroundNonSelectionColor();
  }

  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 = ((DefaultMutableTreeNodevalue)
          .getUserObject();
      if (userObject instanceof Book) {
        Book book = (BookuserObject;
        titleLabel.setText(book.getTitle());
        authorsLabel.setText(book.getAuthors());
        priceLabel.setText("" + book.getPrice());
        if (selected) {
          renderer.setBackground(backgroundSelectionColor);
        else {
          renderer.setBackground(backgroundNonSelectionColor);
        }
        renderer.setEnabled(tree.isEnabled());
        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) {
    this.name = name;
  }

  public NamedVector(String name, Object elements[]) {
    this.name = name;
    for (int i = 0, n = elements.length; i < n; i++) {
      add(elements[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.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.