This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to put Icons or Images in a SWT TreeItem?  RSS feed

 
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: 5167
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"));
}
});
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!