Add your Icon to your renderer class as a field.
Change the value of this field and repaint the JTree.
class CustomTreeCellRenderer extends DefaultTreeCellRenderer{
ImageIcon rendererIcon;
public void setRendererIcon(ImageIcon myIcon){
this.rendererIcon = myIcon;
};
public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus){
super.getTreeCellRendererComponent(tree, value,
selected, expanded, leaf, row, hasFocus);
JLabel label = (JLabel) this ;
label.setIcon( rendererIcon ) ;
return this;
}
}
Edit: explanations
In your case, it is useless to change the model. The icon used to display each of the nodes is a part of the renderer.
The renderer object of the JTree is not forced to be a JComponent
. It has to be an object providing a JComponent
when the code calls getTreeCellRendererComponent
.
In your case, the cast of getCellRenderer() into a JLabel is just plain luck, because the default implementation of the DefaultTreeCellRenderer
is an extension of JLabel
.
And, in fact, as your renderer did call setIcon on itself, it's normal that your getIcon methods gives you the last icon you did put into the renderer.
Full code working :
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultTreeCellRenderer;
public class TestJTree {
private static ImageIcon iconWhite = new ImageIcon("white.jpg");
private static ImageIcon iconBlack = new ImageIcon("black.jpg");;
public static void main(String[] args) {
TestJTree me = new TestJTree();
me.process();
}
private void process() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initGui();
}
});
}
protected void initGui() {
JFrame frame = new JFrame("Test JTree");
frame.setContentPane(new JPanel(new BorderLayout()));
final JTree tree = new JTree();
frame.getContentPane().add(tree);
final CustomTreeCellRenderer renderer = new CustomTreeCellRenderer();
renderer.setRendererIcon(iconWhite);
tree.setCellRenderer(renderer);
JPanel panelButtons = new JPanel();
JButton buttonWhite = new JButton("");
buttonWhite.setIcon(iconWhite);
JButton buttonBlack = new JButton("");
buttonBlack.setIcon(iconBlack);
buttonBlack.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
renderer.setRendererIcon(iconBlack);
tree.repaint();
}
});
panelButtons.add(buttonBlack);
panelButtons.add(buttonWhite);
frame.getContentPane().add(panelButtons,BorderLayout.SOUTH);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
@SuppressWarnings("serial")
private static class CustomTreeCellRenderer extends DefaultTreeCellRenderer{
ImageIcon rendererIcon;
public void setRendererIcon(ImageIcon myIcon){
this.rendererIcon = myIcon;
};
public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus){
Component ret = super.getTreeCellRendererComponent(tree, value,
selected, expanded, leaf, row, hasFocus);
JLabel label = (JLabel) ret ;
label.setIcon( rendererIcon ) ;
return ret;
}
}
}