This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Icon in JTree node  RSS feed

 
Tris Rabar
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???
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!