I have implemented a JTree
using the DefaultTreeModel
.I am storing Message type objects in the tree where I set the node icons initially according to a direction
variable value of the Message. I can do this successfully using the below code.
tcBuilderTree = new JTree(treeModel);
tcBuilderTree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
tcBuilderTree.setCellRenderer(new DefaultTreeCellRenderer(){
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean isLeaf,
int row,
boolean focused) {
Component c = super.getTreeCellRendererComponent(tree, value,
selected, expanded, isLeaf, row, focused);
if(value instanceof CustomMessageTreeNode){
setIcon(((CustomMessageTreeNode)value).
getMessage().getMessageDirectionIcon());
}
return c;
}
});
//implementation of getMessageDirectionIcon()
public ImageIcon getMessageDirectionIcon(){
ImageIcon icon=null;
URL messageIconUrl;
if(msgDirection.equalsIgnoreCase("In")){
messageIconUrl = ToolBar.class.getResource
("/icons/arrowRight.png");
icon=new ImageIcon(messageIconUrl);
}
else if(msgDirection.equalsIgnoreCase("Out")){
messageIconUrl = ToolBar.class.getResource
("/icons/arrowLeft.png");
icon=new ImageIcon(messageIconUrl);
}
return icon;
}
Dynamically I want to let the user select the node and change its direction so that the node icon gets changed. I'm doing it through the foloowing code.
public void toggleDirection(){
CustomMessageTreeNode currentSelectedNode =
(CustomMessageTreeNode) tcBuilderTree.getLastSelectedPathComponent();
Message toggleMessage=currentSelectedNode.getMessage();
tcBuilderTree.setCellRenderer(new DefaultTreeCellRenderer(){
@Override
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean isLeaf,
int row,
boolean focused) {
Component c = super.getTreeCellRendererComponent(tree, value,
selected, expanded, isLeaf, row, focused);
if(value instanceof CustomMessageTreeNode){
String currentDirection=toggleMessage.getMsgDirection();
if(currentDirection.equalsIgnoreCase("In")){
toggleMessage.setMsgDirection("Out");
System.out.println("Direction changed as : Out");
setIcon(toggleMessage.getMessageDirectionIcon());
return c;
}
if(currentDirection.equalsIgnoreCase("Out")){
toggleMessage.setMsgDirection("In");
System.out.println("Direction changed as : In");
setIcon(toggleMessage.getMessageDirectionIcon());
return c;
}
}
return c;
}
});
}
This is my CustomMessageNode
class.
public class CustomMessageTreeNode extends DefaultMutableTreeNode{
private Message message;
public CustomMessageTreeNode(String msgName){
super(msgName);
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
}
Once I change the direction by selecting the node the icon gets changed. But the issue happens whenever I click on some other node or somewhere in my JFrame the icon keeps on changing the direction. I debugged and made sure that the toggleDirection()
function is called only once when I click the node and said to change the direction.
Please let me know why this happens and is there a better way to do this.What is the reason for the node icon to keep on changing when the user changes it once.