Solved

Using JTree and CellRenderers...

Posted on 1998-08-27
5
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
sailwind earned 50 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

739 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