Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using JTree and CellRenderers...

Posted on 1998-08-27
5
Medium Priority
?
276 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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

722 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