Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-27-2008, 08:41 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
TreeCellRenderer Demonstration
Code:
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(300, 300); 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(0, 1)); 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 = ((DefaultMutableTreeNode) value) .getUserObject(); if (userObject instanceof Book) { Book book = (Book) userObject; 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 + "]"; } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JToggleButton Demonstration Java Tip javax.swing 0 06-26-2008 08:38 PM
TreeSet Demonstration Java Tip java.lang 0 04-15-2008 08:34 PM
Demonstration of heaps Java Tip java.lang 0 04-14-2008 09:50 PM
Simple demonstration of HashMap Java Tip java.lang 0 04-12-2008 09:48 PM
Ftp client demonstration Java Tip java.net 0 04-07-2008 09:11 PM


All times are GMT +3. The time now is 02:07 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org