• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Icon in JTree node

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any ideas thus far???
 
Do Re Mi Fa So La Tiny Ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic