Solved

JTree - how to change the Icon on each single leaf

Posted on 2007-03-25
5
597 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

635 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