Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Icon in JTree node

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm having a bit of a problem: I've created a JTree with checkboxes as the nodes, and in addition, I'm tyring to remove the default icons at the nodes and replace them with my own. However, when I then try to get the path of a node selection, I'm getting the following:

Selected: BasicTreeNode@401369
->BasicTreeNode@2e533a->BasicTreeNode@401369
Selected: BasicTreeNode@a0544
->BasicTreeNode@2e533a->BasicTreeNode@a0544
Selected: BasicTreeNode@1946c2
->BasicTreeNode@2e533a->BasicTreeNode@649dcd->BasicTreeNode@1946c2
The code that does this display is as follows:
class MyTreeSelectionListener implements TreeSelectionListener
{
public void valueChanged (TreeSelectionEvent event)
{
TreePath path = event.getPath();// Returns an array of the first path element
System.out.println ("Selected: " +
path.getLastPathComponent());
Object elements[] = path.getPath();
for (int i=0; i<elements.length; i++)
{
System.out.print ("->" + elements[i]);
}
System.out.println ();
}
}

Without the icons, this works fine and gives me the proper path. The code for customizing the tree nodes is:

/*
// header - edit "Data/yourJavaHeader" to customize
// contents - edit "EventHandlers/Java file/onCreate" to customize
//
*/
import javax.swing.*;
public class BasicTreeNode
{
protected String text;
protected Icon icon, open;

public BasicTreeNode(String text, Icon icon)
{
this(text, icon, icon);
}

public BasicTreeNode(String text, Icon icon, Icon open)
{
this.text = text;
this.icon = icon;
this.open = open;
}

public static BasicTreeNode createFixed(String text)
{
return new BasicTreeNode(text,
new ImageIcon("exclaim0b.gif"));
}

public static BasicTreeNode createVar(String text)
{
return new BasicTreeNode(text,
new ImageIcon("bluesml.gif"));
}
public static BasicTreeNode createOpt(String text)
{
return new BasicTreeNode(text,
new ImageIcon("smb_yel.gif"));
}
public static BasicTreeNode createFixedHeading(String text)
{
return new BasicTreeNode(text,
new ImageIcon ("folder.gif"));
}
public static BasicTreeNode createVarHeading(String text)
{
return new BasicTreeNode(text,
new ImageIcon ("blue.gif"));
}
public static BasicTreeNode createOptHeading(String text)
{
return new BasicTreeNode(text,
new ImageIcon ("question_mark.gif"));
}
public static BasicTreeNode createTopHeading(String text)
{
return new BasicTreeNode(text,
new ImageIcon ("param1.gif"));
}
public String getText()
{
return text;
}

public Icon getIcon()
{
return icon;
}

public Icon getOpen()
{
return open;
}
}

And, the checkBox nodes themselves are set by code like the following:
BasicTreeNode topHeading = BasicTreeNode.createTopHeading("Parameter Details");
CheckTreeNode top = new CheckTreeNode(topHeading,false, false);
BasicTreeNode fixedHeading = BasicTreeNode.createFixedHeading("Mandatory Fixed Parameters");

CheckTreeNode top2 = new CheckTreeNode(fixedHeading, false, false);
top.add(top2);

Can anyone help me identify the problem??
Please?
Thanks for your time..
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas thus far???
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!