CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2000
    Location
    Kaysville, UT
    Posts
    228

    Help! Need example of implementing a TreeCellRenderer

    Has anyone out there ever created their own custom class that implemented TreeCellRenderer? I've got this far:

    private class TestRenderer implements TreeCellRenderer
    {
    public Component getTreeCellRendererComponent(JTree tree,
    Object value,
    boolean selected,
    boolean expanded,
    boolean leaf,
    int row,
    boolean hasFocus)
    {
    return null;
    } //end getTreeCellRendererComponent
    } //end testRenderer




    But I have absolutely no idea what to do next. I need to change the way the object contained in the cell is rendered, set Icons, etc, but have no clue as to how to do it. Any help would be appreciated!

    Thanks!


    "There's nothing more dangerous than a resourceful idiot." ---Dilbert
    BWAHAHAHAHAHAHA! ---Murray

  2. #2
    Join Date
    Nov 1999
    Location
    Indianapolis, IN
    Posts
    191

    Re: Help! Need example of implementing a TreeCellRenderer

    Here's an example, I wanted to customize the Tool tips:


    /**
    * Over written to set the tool tips. The tool tip is simply the value, which is
    * the string which gets displayed. This is useful when the string is longer than the
    * on-screen component is wide.
    */
    public java.awt.Component getTreeCellRendererComponent(javax.swing.JTree tree, Object value, boolean sel,
    boolean expanded, boolean leaf, int row, boolean hasFocus)
    {
    super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
    if (true)
    {
    setToolTipText(value.toString());
    }
    else
    {
    setToolTipText(null); //no tool tip
    }

    //Optionally set the icons here
    //if (leaf)
    //{
    //this.setIcon(getDefaultLeafIcon()); //specify which icon heres
    //}
    return this;
    }





  3. #3
    Join Date
    Mar 2000
    Location
    Kaysville, UT
    Posts
    228

    Re: Help! Need example of implementing a TreeCellRenderer

    Thanks for the reply!

    I need my nodes to contain JPanels that will contain JTextAreas of different sizes. Is that even possible?

    "There's nothing more dangerous than a resourceful idiot." ---Dilbert
    BWAHAHAHAHAHAHA! ---Murray

  4. #4
    Join Date
    Sep 2000
    Location
    Melbourne --> Australia
    Posts
    68

    Re: Help! Need example of implementing a TreeCellRenderer

    Hi there
    You dont need to write a class which "implements"
    TreeCellRenderer, you can do it this easier way.


    DefaultTreeCellRenderer dtr = (DefaultTreeCellRenderer)jtree.getCellRenderer();
    //you may have to change the row height of the tree for the gif's ie:
    jtree.setRowHeight(25);
    //then go
    dtr.setLeafNode(new ImageIcon("somthin.gif"));




    Its as simple as that, dtr can do your stuff.

    Phill.


  5. #5
    Join Date
    Nov 1999
    Location
    Indianapolis, IN
    Posts
    191

    Re: Help! Need example of implementing a TreeCellRenderer

    Not sure about the JPanels or JTextAreas with different sizes.......

    It seems that you should be able to use a JPanel, its a subclass of JComponent, as is JLabel. I think you'll just have to try it out....

    Try this:
    http://java.sun.com/docs/books/tutor...ents/tree.html


  6. #6
    Join Date
    Mar 2000
    Location
    Kaysville, UT
    Posts
    228

    Re: Help! Need example of implementing a TreeCellRenderer

    Thanks again for the reply! Actually, I've looked at the link you provided before. In fact, it was the first place I looked for information, but it doesn't go into in-depth detail (one of Sun's shortcomings, if you ask me).

    As far as I can tell, you should be able to add any object to a JTree using a node as the constructor for the nodes specifies that the data added needs only to be of type Object.

    Someone told me that the DefaultTableCellRenderer renders all objects as text, and this does indeed appear to be the case. I was also told that to fix this I would need to create a custom TableCellRenderer to handle the specific type of object I wanted to put into the node. I have absolutely no clue how to do this. It seems that no matter what I do, the renderer always renders objects as text. Is there any way to get it so that it doesn't? How do you write a Renderer that corectly renders objects that aren't text?

    Thanks!

    "There's nothing more dangerous than a resourceful idiot." ---Dilbert
    BWAHAHAHAHAHAHA! ---Murray

  7. #7
    Join Date
    Nov 1999
    Location
    Indianapolis, IN
    Posts
    191

    Re: Help! Need example of implementing a TreeCellRenderer

    I see what you mean. I guess I'm not sure how to accomplish what you want.
    Did you see this example from this website?
    http://codeguru.earthweb.com/java/articles/142.shtml

    Not exactly what you want, but might give some hints.

    Another thought is that I would guess the JTree is similar to a JTable ????? If that pans out, then this link might be of interest:
    http://codeguru.earthweb.com/java/articles/162.shtml

    I agree with Sun's website not going into enough depth. And worse, their problem resolution is non-existant. If every instruction step they give works, then you're ok. Otherwise, you're on your own. below par....

    best of luck to you,


  8. #8
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Help! Need example of implementing a TreeCellRenderer


    > As far as I can tell, you should be able to add any object to a JTree

    You can have any JComponent( with implementation of "TreeCellRenderer" ) as a Node( renderer ) and you can set any Object as a Node value.
    ( To get a desired result , you have to override toString() method in that object )

    > Someone told me that the DefaultTableCellRenderer renders all objects as text

    DefaultTableCellRenderer is a JLabel Component. In a JLabel , you can set an Icon and a Text.

    > I was also told that to fix this I would need to create a custom TableCellRenderer

    Creating a custom rendere is not that hard. All you have to do is , look at the Default renderers and do modify it to your needs.. Here below i have given you a Simple Custom Tree Renderer.

    I didnt have much time to write the below code efficiently. You will find some unwanted code over there. You can Still modify the code for your
    needs. If you need any help , post a comment.


    import javax.swing.*;
    import javax.swing.plaf.ColorUIResource;
    import javax.swing.plaf.FontUIResource;
    import java.awt.*;
    import java.awt.event.*;
    import java.beans.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.tree.*;

    public class CustomTreeCellRenderer extends JPanel implements TreeCellRenderer
    {
    protected boolean selected;
    private boolean hasFocus;
    private boolean drawsFocusBorderAroundIcon;

    transient protected Icon closedIcon;

    transient protected Icon leafIcon;

    transient protected Icon openIcon;

    protected Color textSelectionColor;

    protected Color textNonSelectionColor;

    protected Color backgroundSelectionColor;

    protected Color backgroundNonSelectionColor;

    protected Color borderSelectionColor;
    JTextArea field = new JTextArea();

    public CustomTreeCellRenderer() {
    Object value = UIManager.get("Tree.drawsFocusBorderAroundIcon");
    drawsFocusBorderAroundIcon = (value != null && ((Boolean)value).
    booleanValue());
    setLayout( new GridLayout(1,1));
    add( field );
    field.setBorder( BorderFactory.createEmptyBorder() );
    }


    /**
    * Returns the default icon, for the current laf, that is used to
    * represent non-leaf nodes that are expanded.
    */
    public Icon getDefaultOpenIcon() {
    return UIManager.getIcon("Tree.openIcon");
    }

    /**
    * Returns the default icon, for the current laf, that is used to
    * represent non-leaf nodes that are not expanded.
    */
    public Icon getDefaultClosedIcon() {
    return UIManager.getIcon("Tree.closedIcon");
    }

    /**
    * Returns the default icon, for the current laf, that is used to
    * represent leaf nodes.
    */
    public Icon getDefaultLeafIcon() {
    return UIManager.getIcon("Tree.leafIcon");
    }

    /**
    * Sets the icon used to represent non-leaf nodes that are expanded.
    */
    public void setOpenIcon(Icon newIcon) {
    openIcon = newIcon;
    }

    /**
    * Returns the icon used to represent non-leaf nodes that are expanded.
    */
    public Icon getOpenIcon() {
    return openIcon;
    }

    /**
    * Sets the icon used to represent non-leaf nodes that are not expanded.
    */
    public void setClosedIcon(Icon newIcon) {
    closedIcon = newIcon;
    }

    /**
    * Returns the icon used to represent non-leaf nodes that are not
    * expanded.
    */
    public Icon getClosedIcon() {
    return closedIcon;
    }

    /**
    * Sets the icon used to represent leaf nodes.
    */
    public void setLeafIcon(Icon newIcon) {
    leafIcon = newIcon;
    }

    /**
    * Returns the icon used to represent leaf nodes.
    */
    public Icon getLeafIcon() {
    return leafIcon;
    }

    /**
    * Sets the color the text is drawn with when the node is selected.
    */
    public void setTextSelectionColor(Color newColor) {
    textSelectionColor = newColor;
    }

    /**
    * Returns the color the text is drawn with when the node is selected.
    */
    public Color getTextSelectionColor() {
    return textSelectionColor;
    }

    /**
    * Sets the color the text is drawn with when the node isn't selected.
    */
    public void setTextNonSelectionColor(Color newColor) {
    textNonSelectionColor = newColor;
    }

    /**
    * Returns the color the text is drawn with when the node isn't selected.
    */
    public Color getTextNonSelectionColor() {
    return textNonSelectionColor;
    }

    /**
    * Sets the color to use for the background if node is selected.
    */
    public void setBackgroundSelectionColor(Color newColor) {
    backgroundSelectionColor = newColor;
    }


    /**
    * Returns the color to use for the background if node is selected.
    */
    public Color getBackgroundSelectionColor() {
    return backgroundSelectionColor;
    }

    /**
    * Sets the background color to be used for non selected nodes.
    */
    public void setBackgroundNonSelectionColor(Color newColor) {
    backgroundNonSelectionColor = newColor;
    }

    /**
    * Returns the background color to be used for non selected nodes.
    */
    public Color getBackgroundNonSelectionColor() {
    return backgroundNonSelectionColor;
    }

    /**
    * Sets the color to use for the border.
    */
    public void setBorderSelectionColor(Color newColor) {
    borderSelectionColor = newColor;
    }

    /**
    * Returns the color the border is drawn.
    */
    public Color getBorderSelectionColor() {
    return borderSelectionColor;
    }

    /**
    * Subclassed to only accept the font if it isn't a FontUIResource.
    */
    public void setFont(Font font) {
    if(font instanceof FontUIResource)
    font = null;
    super.setFont(font);
    }

    /**
    * Subclassed to only accept the color if it isn't a ColorUIResource.
    */
    public void setBackground(Color color) {
    if(color instanceof ColorUIResource)
    color = null;
    super.setBackground(color);
    }

    /**
    * Configures the renderer based on the passed in components.
    * The value is set from messaging value with toString().
    * The foreground color is set based on the selection and the icon
    * is set based on on leaf and expanded.
    */
    public Component getTreeCellRendererComponent(JTree tree, Object value,
    boolean sel,
    boolean expanded,
    boolean leaf, int row,
    boolean hasFocus) {
    String stringValue = tree.convertValueToText(value, sel,
    expanded, leaf, row, hasFocus);

    this.hasFocus = hasFocus;
    field.setText(stringValue);
    if(sel){
    field.setForeground(Color.white);
    field.setBackground(Color.blue);
    }
    else{
    field.setForeground(Color.black);
    field.setBackground(Color.white);
    }
    if (!tree.isEnabled()) {
    field.setEnabled(false);
    }
    else {
    field.setEnabled(true);
    }
    if (hasFocus) {
    this.setBorder( BorderFactory.createLineBorder( Color.blue ) );
    }else{
    this.setBorder( BorderFactory.createEmptyBorder() );
    }

    selected = sel;

    return this;
    }

    public Dimension getPreferredSize() {
    Dimension retDimension = field.getPreferredSize();

    if(retDimension != null)
    retDimension = new Dimension(retDimension.width + 3,
    retDimension.height);
    return retDimension;
    }

    }






  9. #9
    Join Date
    Mar 2000
    Location
    Kaysville, UT
    Posts
    228

    Re: Help! Need example of implementing a TreeCellRenderer

    Thanks for the reply!

    Yestarday afternoon I managed to get a renderer together that partially works. The problem is that the objects the tree contains (EditorWindows which extend JPanel) are not editable in any way, shape, or form once they're added to the nodes of the JTree. Objects (JTextFields, etc) created by the EW constructor and added to itself are visible, but unusable (cant type in them, etc.).

    My (basic) renderer looks like this:


    private class TestRenderer implements TreeCellRenderer
    {
    private EditorWindow ew;

    public TestRenderer()
    {
    ew = new EditorWindow();
    ew.setBackground(Color.white);
    } //end TestRenderer

    public Component getTreeCellRendererComponent(JTree tree,
    Object value,
    boolean selected,
    boolean expanded,
    boolean leaf,
    int row,
    boolean hasFocus)
    {
    return ew;
    } //end getTreeCellRendererComponent
    } //end testRenderer




    Is there anything else I should be doing?

    "There's nothing more dangerous than a resourceful idiot." ---Dilbert
    BWAHAHAHAHAHAHA! ---Murray

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured