Solved

Using JTree and CellRenderers...

Posted on 1998-08-27
5
275 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

622 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