• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to put Icons or Images in a SWT TreeItem?

 
Mike Shreek
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I need to put Icons on a SWT TreeItem. is it by using setImage? But I don't know how to use it... please help me. thanks!
 
Arun Chidam
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Shreek wrote:Hi, I need to put Icons on a SWT TreeItem. is it by using setImage? But I don't know how to use it... please help me. thanks!


Hi,

Try the below code

DefaultTreeCellRenderer renderer3 = new DefaultTreeCellRenderer();
renderer3.setOpenIcon(customOpenIcon);
renderer3.setClosedIcon(customClosedIcon);
renderer3.setLeafIcon(customLeafIcon);
tree.setCellRenderer(renderer3);

where customOpenIcon --> Icon customOpenIcon = new ImageIcon("C:/toolbarbuttongraphics/general/test_logo0.gif");

 
Darryl Burke
Bartender
Posts: 5152
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun, the question was about the SWT TreeItem, not a Swing JTree.
 
Arun Chidam
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry my mistake....for SWT use the below code....

tree.addTreeListener(new TreeListener() {//add your tree to TreeListener then override tree expanded and collapsed to include your custmomized icon
public void treeExpanded(TreeEvent e) {
TreeItem ti = (TreeItem) e.item;
ti.setImage(new Image(d, "C:/toolbarbuttongraphics/general/test_logo0.gif"));
}

public void treeCollapsed(TreeEvent e) {
TreeItem ti = (TreeItem) e.item;
ti.setImage(new Image(d, "C:/toolbarbuttongraphics/general/test_logo1.gif"));
}
});
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!