Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using JTree and CellRenderers...

Posted on 1998-08-27
5
Medium Priority
?
278 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 100 total points
ID: 1222663
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
ID: 1222664
How can I do this for a JList?
0
 

Author Comment

by:beemer
ID: 1222665
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
ID: 1222666
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
ID: 1222667
When I do a System.out.println(pathObj), it is just the String text of the JCheckBox
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

916 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