Solved

Using JTree and CellRenderers...

Posted on 1998-08-27
5
268 Views
Last Modified: 2012-05-05
I am using a JTree component and I have my own custom TreeCellRenderer:

import com.sun.java.swing.tree.*;
import java.awt.*;
import com.sun.java.swing.*;

public class MyTreeCellRenderer extends Object implements TreeCellRenderer
{
    public MyTreeCellRenderer()
    {
        super();
    }
   
    public Component getTreeCellRendererComponent(JTree tree, Object value,
                         boolean selected, boolean expanded,
                         boolean leaf, int row, boolean hasFocus)
    {
        m_node.setFont(tree.getFont());
        m_node.setBackground(tree.getBackground());
        m_node.setText(value.toString());
       
        return m_node;
    }
   
    private JCheckBox   m_node = new JCheckBox();
}

Whenever I click on a node in the tree, I want to be able to get the JCheckBox object (which is the node that was clicked), so that I can set it to be selected or not selected.  How can I do this?
0
Comment
Question by:beemer
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
sailwind earned 50 total points
Comment Utility
For your tree component you should have an treeSelectionListener that
watches for the tree events. In the listener (preferrably the tree itself),
define the valueChanged as follows:

Let's say we have a JTree tree.

          public void valueChanged(TreeSelectionEvent e)
          {
              Object pathObj;
              
              // get selected item
   try {
     pathObj = tree.getSelectionPath().getLastPathComponent();
   } catch (NullPointerException ne) {return;}
}

The pathObj is the object that you're trying to get.
0
 

Author Comment

by:beemer
Comment Utility
How can I do this for a JList?
0
 

Author Comment

by:beemer
Comment Utility
I tried your idea, and it did not work.

public void valueChanged(TreeSelectionEvent e)
      {
          Object pathObj;
            try
            {
                  pathObj = tree.getSelectionPath().getLastPathComponent();
                  if (pathObj instanceof JCheckBox)
                  {
                        System.out.println("is a JCheckBox");
                  }
            } catch (NullPointerException ne) {return;}
      }

It never printed out that it is a JCheckBox.

Here is my renderer class:

import com.sun.java.swing.tree.*;
import java.awt.*;
import com.sun.java.swing.*;

public class MyTreeCellRenderer extends Object implements TreeCellRenderer
{
    public MyTreeCellRenderer(TreeFrame frame)
    {
        super();
        this.frame = frame;
    }
   
    public Component getTreeCellRendererComponent(JTree tree, Object value,
                         boolean selected, boolean expanded,
                         boolean leaf, int row, boolean hasFocus)
    {
        m_node.setFont(tree.getFont());
        m_node.setBackground(tree.getBackground());
        m_node.setText(value.toString());
       
        return m_node;
    }
   
    private JCheckBox   m_node = new JCheckBox();
    private TreeFrame   frame;
}
0
 
LVL 3

Expert Comment

by:sailwind
Comment Utility
1. Did you ever register the class as a listener? Namely, you need to invoke:
    tree.addTreeSelectionListener(this);  // where 'this' is the listener

2. If it's not giving you a checkbox object back, do a
    System.out.println(pathObj) to see what type of object it's returning to you.

You can get the current selected List item by invoking:

list.getSelectedValue();
0
 

Author Comment

by:beemer
Comment Utility
When I do a System.out.println(pathObj), it is just the String text of the JCheckBox
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now