1

I have a JTree with several nodes and each node has a different associated JPanel I want to display to the user. I've made use of a custom TreeCellRenderer and my code currently works as is, but (I think) it's a little too clunky and I have concerns about it

1) If I just leave a tree node selected, my code will keep refreshing the JPanel over and over again. Once the selected node has displayed the JPanel once, I don't want it to again unless another node is selected in between.

2) If the user selects a different node (let's say node2) while my timer is running for node1, I don't want to bother displaying node1's JPanel since the user has navigated away from it.

Here is my code of interest:

Component ret = super.getTreeCellRendererComponent(tree, value,
            selected, expanded, leaf, row, hasFocus);

    DefaultMutableTreeNode entry = (DefaultMutableTreeNode)value;


    // if the node is selected
    if(sel)
    {
// set the background of the node
        setBackgroundNonSelectionColor(new Color(0x91, 0xC5, 0xFF));
// display the JPanel for the node
        displayPanel(entry);
    }
    else
    {
// if the node isn't selected then no background selection color
        setBackgroundNonSelectionColor(Color.WHITE);
    }


    return ret;

displayPanel function:

// display the node's JPanel after sleeping for 1s
private void displayPanel(final DefaultMutableTreeNode entry)
{
    Thread thr = new Thread(){

        public void run(){

                // sleep for 1000ms
                    CF.sleep("1000");

// display the panel for this node
                    CF.displayPanel(entry);
            }
          };
    thr.start();
}

To be honest, DefaultTreeCellRenderer is still a little confusing to me. I'd appreciate any advice on how to make this run efficiently.

2
  • The context of the first piece of code isn't very clear, where is it contained? Also, why don't just use the tree selection events to choose with JPanel to show?
    – asermax
    Mar 1, 2013 at 18:53
  • Sorry, the code is within my class that extends DefaultTreeCellRenderer. I can include the rest of the code if you want some more context. I didn't think of using the tree selection event since I'm not currently listening for an action there, but that is probably a better idea.
    – Matt
    Mar 1, 2013 at 18:56

2 Answers 2

2

Maybe I'm missing something here but couldn't you just use a Tree Selection listener on the tree?

tree = new JTree(treeModel);
tree.addTreeSelectionListener(new TreeSelectionListener() {     
    public void valueChanged(TreeSelectionEvent arg0) {
       //Code here to get selection and display panel
    }
});
3
  • Yea, I overthought this and made it overly complicated. Will accept your answer in 5 minutes. Thanks
    – Matt
    Mar 1, 2013 at 18:57
  • Yep, already got it working exactly how I'd like with TreeSelectionListener. Feel stupid for asking this question now, but will post my solution incase anyone else struggles with this.
    – Matt
    Mar 1, 2013 at 19:09
  • @Matt Could you kindly direct me to where you have posted your solution of this as I have been struggling with it for more than enough days to last me on this earth? I would hate to ask the same question when this thread is still active and has the issue solved. Oct 28, 2015 at 13:06
1

TreeCellRenderer are meant for "rendering" purposes. What is the representation of a given object? Is it a label? Is it a checkbox? Something more complex? Basically it is used to "print" the representation of each node of the JTree on the display. Rendering may occur many times, at unpredictable moments, and is therefore not a good place to listen for selection.

What you are looking for is a TreeSelectionListener which has a single method to implement and which will be triggered every time the selection of the JTree changes.

Add your listener with javax.swing.JTree.getSelectionModel().addTreeSelectionListener().

final JTree tree = ...;
tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
    public void valueChanged(TreeSelectionEvent e) {
         if (tree.getSelectionPath()!=null)
             displayPanel((DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent());
    }
});
1
  • Hi Guillaume, I'm not sure if you can send a msg on StackOverflow, but thank you for directing me to invokeLater and not just downvoting me. I currently update my GUI from all over my program without the use of invokeLater. I will make sure to parse throughout my code and update.
    – Matt
    Mar 1, 2013 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.