Solved

JTree - how to change the Icon on each single leaf

Posted on 2007-03-25
5
573 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:
Mayank S earned 250 total points
ID: 18789654
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
ID: 18790276
0
 

Author Comment

by:arempka
ID: 18806175
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
ID: 18807772
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:Mayank S
ID: 18807910
Try loading it as a resource using getClass ().getResource ()
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

776 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