Solved

JTree - how to change the Icon on each single leaf

Posted on 2007-03-25
5
565 Views
Last Modified: 2013-12-29
Hi,
in my JTree I would like to change the leaf icon depending on the status that the entry currenty has.
Example:
....
ImageIcon leafIcon_nio = createImageIcon("images/nio.png");
renderer_nio = new TreeCellRenderer();
renderer_nio = new DefaultTreeCellRenderer();

for(int werke=0;werke<anz_werke.size();werke++){
      int i=0;
      category = new DefaultMutableTreeNode(anz_werke.elementAt(werke));
      top.add(category);
      while(i<V_KUNDENWERK.size()){
            if(V_KUNDENWERK.elementAt(i).equals(anz_werke.elementAt(werke))){
--------------->
                                if(V_erledigt.elementAt(i).toString().equalsIgnoreCase("Ja")){
                                        renderer_io.setLeafIcon(leafIcon_io);
                                }
                              else
                              {
                                        renderer_io.setLeafIcon(leafIcon_io);
                              }
--------------->
                  book = new DefaultMutableTreeNode("QST:"+(String)V_QEC_QST_NR.elementAt(i));
                  category.add(book);
            }
            i++;
      }
}

I know, it's wrong (it doesn't work) but I don't know how to change.
I got only all Icons changed but thats not the target.

Who can help me?

0
Comment
Question by:arempka
  • 2
  • 2
5 Comments
 
LVL 30

Accepted Solution

by:
mayankeagle earned 250 total points
Comment Utility
You can implement TreeCellRenderer or extend DefaultTreeCellRenderer. Use the source code provided:

http://forum.java.sun.com/thread.jspa?threadID=262758&messageID=1712649
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 

Author Comment

by:arempka
Comment Utility
Hello mayankeagle,

SimpleCellRenderer is exactly what I was serching for.
Only Problem:
In the main application I implemented a ImageIcon with the path "images/io.png".
This image was found and shown when starting from the .jar File.
Now, with the same Path in the SimpleCellRenderer it is working in the eclipse environment and command line start but the Images were not when starting from .jar- File.

What's wrong?

Regards

Arempka
0
 

Author Comment

by:arempka
Comment Utility
Hello mayankeagle,

SimpleCellRenderer is exactly what I was serching for.
Only Problem:
In the main application I implemented a ImageIcon with the path "images/io.png".
This image was found and shown when starting from the .jar File.
Now, with the same Path in the SimpleCellRenderer it is working in the eclipse environment and command line start but the Images were not when starting from .jar- File.

What's wrong?

Regards

Arempka
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Try loading it as a resource using getClass ().getResource ()
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now