Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTree image node

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to set an image icon as the tree node, not leaf icon


Thanks
 
BBird
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final Image pic0 = new Image(display, "pic0.jpg");
final Image pic1 = new Image(display, "pic2.jpg");
final Image pic2 = new Image(display, "pic2.jpg");

final Tree x = new Tree(sShell, SWT.MULTI | SWT.BORDER);
x.setSize(140, 180);
x.setLocation(10, 10);

TreeItem root = new TreeItem(x, SWT.NONE);
root.setText("My Album");
root.setImage(pic0);

TreeItem t1 = new TreeItem(root, SWT.NONE);
t1.setText("Sub Album 1");
t1.setImage(pic1);

TreeItem t2 = new TreeItem(root, SWT.NONE);
t2.setText("Sub Album 2");
t2.setImage(pic2);
------------------

Hope this helps...
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have pure Swing version ?

Thanks
 
BBird
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops!....sorry,...i am dealling with SWT...
Well, by the way, I am developping the application like ACDSee and I am two-day struck on loading image file.
I mean....dynamic loadding some pix from the folder located on C:\ or A:\, for example, ...Do you have any idea?

Thanx,
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About image node in JTree.
I have found an acceptable , even not perfect, solution. The key point is ,TreeCellRender is a subclass of JLabel, that mean, we can use simple setIcon, setText function of JLabel. If we want more control, we should think how to build our custom JLabel.

about load the image.
Actually I can't fully understand your question. Maybe the problem is , the image path can't be found. I guess you are using Eclipse, if this, loading image will be easy. you can use Eclipse VEP to add icon to JLabel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic