Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to put Icons or Images in a SWT TreeItem?

 
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!
 
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");

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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"));
}
});
 
You got style baby! More than this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic