-1

I saw a lot of postings & questions regarding the coloring issue of the JTree. But I did not find any solution for my specific problem.

I want to select a node and highlight it in case of clicking one my self-made activate button and undo the highlighting by clicking the deactivate button.

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Motor testing");
    root.add(new DefaultMutableTreeNode("Option 1000 RPM"));
    root.add(new DefaultMutableTreeNode("Option 2000 RPM"));
    //--------------- activate button
    JButton btnNewButton = new JButton("Activate");
    btnNewButton.addActionListener(new ActionListener() {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    //.... essential rest

    }

I tried many things, like modifying the DefaultTreeCellRenderer. But I was not able to solve the problem.

Edit:

I tried following lines:

btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
                DefaultMutableTreeNode node =  (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                ((MyTreeCellRenderer) tree.getCellRenderer()).activateLeaf = true;
         }
    }

and:

public class MyTreeCellRenderer extends DefaultTreeCellRenderer {


public boolean activateLeaf = false;

@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
        boolean sel, boolean exp, boolean leaf, int row, boolean hasFocus) {
    super.getTreeCellRendererComponent(tree, value, sel, exp, leaf, row, hasFocus);    

   if(activateLeaf)
       setForeground(Color.GREEN);

   return this;
}

But the nodes turn only green if i change the selection after I pressed the activate button. And if I reset the flag immediatley after it has been set, nothing happens. I want a solution which is able to modify specific nodes, and which is able to run recursivley through every parent node.

I also want to be able to change multiple selections not only one.

Synaps
  • 323
  • 5
  • 14
  • 1
    *"I tried many things, like modifing the DefaultTreeCellRenderer."* Try posting a [mcve] of that attempt. *"But i was not able to solve the problem."* Why not? Given we are not mind readers, it makes sense to be clear and specific about what went wrong with your attempt, and where you got stuck. I might vote to close this as either 'lacks MCVE' or 'unclear what you are asking' but will reserve doing so for the moment. – Andrew Thompson Jul 29 '15 at 11:19
  • 1
    You are saying, that you saw other related postings and already tried some stuff on your own. It would be really helpfull, if you could state, exactly, what you have tried (show the code) and what the error was or why other Q&A s didn't apply to your problem. – MikeMB Jul 29 '15 at 11:20
  • 1
    @MikeMB *"..or why other Q&A s didn't apply to your problem."* That reminds me.. That too, but I'd change **or** for **and**. So provide 1) [mcve] 2) Information on specifically why it failed.3) The links to the questions reviewed and a brief comment as to why each failed to solve this minor variant of a common problem. – Andrew Thompson Jul 29 '15 at 11:23
  • i have edited my question, thank you for your fast response – Synaps Jul 29 '15 at 11:45
  • *"i have edited my question,"* It's a pity you did not make more effort to understand what a [mcve] is. :( – Andrew Thompson Jul 29 '15 at 19:35

1 Answers1

1

Add a tree.repaint() call to the actionListener for the button.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28