Article View: borland.public.jbuilder.java.swing
Article #10999Re: Icons for each node in a Tree
From: Firoz T M
Date: Mon, 26 Jul 1999 00:00
Date: Mon, 26 Jul 1999 00:00
646 lines
19310 bytes
19310 bytes
Hi,
Thank you 4 U're reply...
I tried the same, Still itz not working.... first of all am using a
Dynamic Tree. Thatz the Tree nodes are created dynamically.... am
attaching the entire Tree and Initialization code along with this..
Please have a look and Let me know the problem I made.....
Thanks in advance...
package ArrManProto;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
//MY Tree
public class WFCTree extends JPanel {
protected Hashtable icons;
protected IconNode rootNode;
protected DefaultTreeModel treeModel;
protected WFCTreeRenderer treeRenderer;
protected JTree tree;
private Toolkit toolkit = Toolkit.getDefaultToolkit();
public WFCTree() {
icons = new Hashtable();
makeIcons();
rootNode = new IconNode("WFC Root Node (Firoz)");
rootNode.setIcon(getIcon("unknown"));
treeModel = new DefaultTreeModel(rootNode);
treeModel.addTreeModelListener(new MyTreeModelListener());
tree = new JTree(treeModel);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true);
treeRenderer = new WFCTreeRenderer();
tree.setCellRenderer(treeRenderer);
JScrollPane scrollPane = new JScrollPane(tree);
setLayout(new GridLayout(1,0));
add(scrollPane);
}
public void setEditable(boolean editable) {
tree.setEditable(editable);
}
public void showRoot(boolean showroot) {
tree.setRootVisible(showroot);
}
public void setCellRenderer() {
tree.setCellRenderer(treeRenderer);
}
public DefaultMutableTreeNode getRootNode() {
return rootNode;
}
/** Remove all nodes except the root node. */
public void clear() {
rootNode.removeAllChildren();
treeModel.reload();
}
/** Remove the currently selected node. */
public void removeCurrentNode() {
TreePath currentSelection = tree.getSelectionPath();
if (currentSelection != null) {
DefaultMutableTreeNode currentNode =
(DefaultMutableTreeNode)
(currentSelection.getLastPathComponent());
MutableTreeNode parent =
(MutableTreeNode)(currentNode.getParent());
if (parent != null) {
treeModel.removeNodeFromParent(currentNode);
return;
}
}
// Either there was no selection, or the root was selected.
toolkit.beep();
}
/** Add child to the currently selected node. */
public DefaultMutableTreeNode addObject(Object child, Integer type)
{
DefaultMutableTreeNode parentNode = null;
TreePath parentPath = tree.getSelectionPath();
if (parentPath == null) {
parentNode = rootNode;
} else {
parentNode = (DefaultMutableTreeNode)
(parentPath.getLastPathComponent());
}
return addObject(parentNode, child, true, type);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode
parent,
Object child, Integer type)
{
return addObject(parent, child, false, type);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode
parent,
Object child,
boolean shouldBeVisible,
Integer type) {
WFCIconNode childNode = new WFCIconNode(child);
childNode.setIcon(getIcon(child));
if (parent == null) {
parent = rootNode;
}
treeModel.insertNodeInto(childNode, parent,
parent.getChildCount());
// Make sure the user can see the lovely new node.
if (shouldBeVisible) {
tree.scrollPathToVisible(new TreePath(childNode.getPath()));
}
return childNode;
}
private String getIconName(Object name)
{
String nodeInfo = (String)name;
if(nodeInfo.startsWith("Logical Drive"))
{
return "logical";
}
if(nodeInfo.startsWith("Miscellaneous"))
{
return "miscellaneous";
}
if(nodeInfo.startsWith("Global Standby"))
{
return "global";
}
if(nodeInfo.startsWith("Unused Disks"))
{
return "unused";
}
if(nodeInfo.startsWith("Bad Disks"))
{
return "baddisk";
}
if(nodeInfo.startsWith("Physical Disk"))
{
return "physical";
}
if(nodeInfo.startsWith("LUN"))
{
return "luns";
}
return "unknown";
}
private ImageIcon getIcon(Object name) {
String s = getIconName(name);
System.out.println("Getting icon for " + s + "(" + name +
")");
return (ImageIcon)(icons.get(s));
}
private void makeIcons() {
icons.put("physical", new ImageIcon("physical.gif"));
icons.put("logical" , new ImageIcon("logical.gif"));
icons.put("miscellaneous", new ImageIcon("miscellaneous.gif"));
icons.put("global", new ImageIcon("globalstandby.gif"));
icons.put("unused", new ImageIcon("unuseddisks.gif"));
icons.put("baddisk", new ImageIcon("baddisks.gif"));
icons.put("luns", new ImageIcon("luns.gif"));
icons.put("unknown", new ImageIcon("unknown.gif"));
}
class MyTreeModelListener implements TreeModelListener {
public void treeNodesChanged(TreeModelEvent e) {
DefaultMutableTreeNode node;
node = (DefaultMutableTreeNode)
(e.getTreePath().getLastPathComponent());
/*
* If the event lists children, then the changed
* node is the child of the node we've already
* gotten. Otherwise, the changed node and the
* specified node are the same.
*/
try {
int index = e.getChildIndices()[0];
node = (DefaultMutableTreeNode)
(node.getChildAt(index));
} catch (NullPointerException exc) {}
System.out.println("The user has finished editing the
node.");
System.out.println("New value: " + node.getUserObject());
}
public void treeNodesInserted(TreeModelEvent e) {
}
public void treeNodesRemoved(TreeModelEvent e) {
}
public void treeStructureChanged(TreeModelEvent e) {
}
}
private class WFCTreeRenderer extends JLabel implements
TreeCellRenderer
{
protected ImageIcon Icon1;
protected final Color SelectedBackgroundColor =
SystemColor.textHighlight;//new Color(0, 0, 128);
// Whether or not the item that was last configured is selected.
protected boolean selected;
public WFCTreeRenderer()
{
}
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf, int row,
boolean hasFocus)
{
Font font;
String stringValue =
tree.convertValueToText(value, selected,
expanded, leaf,
row, hasFocus);
String val = value.toString();
/* Set the text. */
setText(stringValue);
/* Tooltips used by the tree. */
setToolTipText(stringValue);
Icon1 = (ImageIcon)(((WFCIconNode)value).getIcon());
if (Icon1 == null)
{
String name = ((WFCIconNode)value).getIconName();
if (name != null)
{
Icon1 = (ImageIcon)icons.get(name);
if (Icon1 != null)
{
System.out.println("Setting Icon for " + value);
setIcon(Icon1);
}
}
}
else
{
//System.out.println("Setting Icon for " + value);
setIcon(Icon1);
}
this.selected = selected;
return this;
}
public void paint(Graphics g)
{
Color bColor;
Icon currentI = Icon1;
if(selected)
bColor = SelectedBackgroundColor;
else if(getParent() != null)
bColor = getParent().getBackground();
else
bColor = getBackground();
g.setColor(bColor);
if(currentI != null && getText() != null)
{
int offset = (currentI.getIconWidth() + getIconTextGap());
g.fillRect(offset, 0, getWidth() - 1 - offset,
getHeight() - 1);
}
else
g.fillRect(0, 0, getWidth()-1, getHeight()-1);
super.paint(g);
}
}
}
//Icon Node....
public class WFCIconNode extends DefaultMutableTreeNode {
protected Icon icon;
protected String iconName;
public WFCIconNode() {
this(null);
}
public WFCIconNode(Object userObject) {
this(userObject, true, null);
}
public WFCIconNode(Object userObject, boolean allowsChildren
, Icon icon) {
super(userObject, allowsChildren);
this.icon = icon;
}
public void setIcon(Icon icon) {
this.icon = icon;
}
public Icon getIcon() {
return icon;
}
public String getIconName()
{
return iconName;
}
public void setIconName(String name) {
iconName = name;
}
}
public class WFCFrame extends JFrame
{
WFCTree jTree1 = new WFCTree();
........
........
public WFCFrame()
{
jInitizlizeTree();
}
.............
.............
.............
//Code snippet to initialize the TREE
void jInitializeTree()
{
String mainNodes[] = new String[4];
mainNodes[0] = new String("Logical Drive 1");
mainNodes[1] = new String("Logical Drive 2");
mainNodes[2] = new String("Logical Drive 3");
mainNodes[3] = new String("Miscellaneous");
String logicalDrives[] = new String[3];
logicalDrives[0] = new String("Global Standby");
logicalDrives[1] = new String("Unused Disks");
logicalDrives[2] = new String("Bad Disks");
String physicalDisks[] = new String[18];
physicalDisks[0] = new String("Physical Disk 1");
physicalDisks[1] = new String("Physical Disk 2");
physicalDisks[2] = new String("Physical Disk 3");
physicalDisks[3] = new String("Physical Disk 4");
physicalDisks[4] = new String("Physical Disk 5");
physicalDisks[5] = new String("Physical Disk 6");
physicalDisks[6] = new String("Physical Disk 7");
physicalDisks[7] = new String("Physical Disk 8");
physicalDisks[8] = new String("Physical Disk 9");
physicalDisks[9] = new String("Physical Disk 10");
physicalDisks[10] = new String("Physical Disk 11");
physicalDisks[11] = new String("Physical Disk 12");
physicalDisks[12] = new String("Physical Disk 13");
physicalDisks[13] = new String("Physical Disk 14");
physicalDisks[14] = new String("Physical Disk 15");
physicalDisks[15] = new String("Physical Disk 16");
physicalDisks[16] = new String("Physical Disk 17");
physicalDisks[17] = new String("Physical Disk 18");
String luns[] = new String[6];
luns[0] = new String("LUN 1");
luns[1] = new String("LUN 2");
luns[2] = new String("LUN 3");
luns[3] = new String("LUN 4");
luns[4] = new String("LUN 5");
luns[5] = new String("LUN 6");
int physicalDiskIndex = 0;
jTree1.clear();
jTree1.setEditable(false);
jTree1.showRoot(false);
for(int i = 0; i < 3; i++)
{
DefaultMutableTreeNode node1 =
jTree1.addObject(jTree1.getRootNode(), mainNodes[i], true, new
Integer(0));
for(int j = 0; j < 3; j++)
{
DefaultMutableTreeNode phDiskNode = jTree1.addObject(node1,
physicalDisks[physicalDiskIndex], true, new Integer(1));
for(int k = 0; k < 4; k++)
{
jTree1.addObject(phDiskNode, luns[k], true, new Integer(6));
}
physicalDiskIndex++;
}
}
}
}
-Firoz
Jonathan wrote:
>
> this is the code i wrote, firstly you should be extending Jlabel and
> implementing the tree Renderer interface,
>
> this will work, if you need any help just ask:
>
> public class treeRenderer extends JLabel implements TreeCellRenderer
> {
> static protected ImageIcon Icon1;
> static protected final Color SelectedBackgroundColor =
> SystemColor.textHighlight;//new Color(0, 0, 128);
>
> /** Whether or not the item that was last configured is selected. */
> protected boolean selected;
>
> public treeRenderer(){
> try {
> defaultFont = new Font("SansSerif", 0, 12);
> } catch (Exception e) {}
> try {
> Icon1 = new ImageIcon(<name of icon>.gif);
>
> // you can have as many icons as you want
>
> } catch (Exception e) {
> System.out.println("Couldn't load images: " + e);
> }
> }
>
> public Component getTreeCellRendererComponent(JTree tree, Object
> value,
> boolean selected, boolean expanded,
> boolean leaf, int row,
> boolean hasFocus) {
> Font font;
> String stringValue = tree.convertValueToText(value, selected,
> expanded, leaf, row, hasFocus);
> String val = value.toString();
>
> /* Set the text. */
> setText(stringValue);
> /* Tooltips used by the tree. */
> setToolTipText(stringValue);
> String t =stringToken(val);
> if(<some thing>)
> setIcon(Icon1);
> else if(<something>)
> ..
> ..
>
> /* Update the selected flag for the next paint. */
> this.selected = selected;
>
> return this;
> }
> public void paint(Graphics g) {
> Color bColor;
> Icon currentI = getIcon();
>
> if(selected)
> bColor = SelectedBackgroundColor;
> else if(getParent() != null)
>
> bColor = getParent().getBackground();
> else
> bColor = getBackground();
> g.setColor(bColor);
> if(currentI != null && getText() != null) {
> int offset = (currentI.getIconWidth() + getIconTextGap());
>
> g.fillRect(offset, 0, getWidth() - 1 - offset,
> getHeight() - 1);
> }
> else
> g.fillRect(0, 0, getWidth()-1, getHeight()-1);
> super.paint(g);
> }
> }
>
> Firoz T M wrote in message <379871C9.588B936C@procsys.com>...
> >Hi,
> >
> > am porting an existing C++ app to Java. This application contains a
> >TREE CONTROL. Each main node is having a different ICONS....
> >
> > How can I achieve this???? If any one know, please let me know...
> >
> > Inorder to achieve this, I wrote a class which uses JTree in it..
> >This class uses My own derived Renderer.... But when using this, itz not
> >displaying any icons....
> >
> > Any help is appreciated.....
> >
> >
> >The Render class is as follows...
> >----------------------------------
> >
> >
> > class WFCTreeRenderer extends DefaultTreeCellRenderer {
> > public WFCTreeRenderer() {
> > }
> >
> > public Component getTreeCellRendererComponent(
> > JTree tree,
> > Object value,
> > boolean sel,
> > boolean expanded,
> > boolean leaf,
> > int row,
> > boolean hasFocus) {
> >
> >
> > super.getTreeCellRendererComponent(tree, value,
> > sel, expanded, leaf, row, hasFocus);
> >
> > Icon icon = ((IconNode)value).getIcon();
> >
> > if (icon == null)
> > {
> > String name = ((IconNode)value).getIconName();
> > if (name != null)
> > {
> > icon = (Icon)icons.get(name);
> > if (icon != null)
> > {
> > setIcon(icon);
> > }
> > }
> > }
> > else
> > {
> > setIcon(icon);
> > }
> >
> > return this;
> > }
> >
> >
> > }
> >
> >IconNode.java
> >-------------
> >
> >import java.util.*;
> >import javax.swing.*;
> >import javax.swing.tree.*;
> >
> >
> >public class IconNode extends DefaultMutableTreeNode {
> >
> > protected Icon icon;
> > protected String iconName;
> >
> > public IconNode() {
> > this(null);
> > }
> >
> > public IconNode(Object userObject) {
> > this(userObject, true, null);
> > }
> >
> > public IconNode(Object userObject, boolean allowsChildren
> > , Icon icon) {
> > super(userObject, allowsChildren);
> > this.icon = icon;
> > }
> >
> >
> >
> > public void setIcon(Icon icon) {
> > this.icon = icon;
> > }
> >
> > public Icon getIcon() {
> > return icon;
> > }
> >
> > public String getIconName()
> > {
> > return iconName;
> > }
> >
> > public void setIconName(String name) {
> > iconName = name;
> > }
> >
> >}
> >
> >
> >
> >
> >
> >--
> >----------------------------------------------------------------
> >FIROZ T M firoz@procsys.com
> >----------------------------------------------------------------
> >
> >Quote for the day
> >Please Tell Me if you Don't Get This Message
> >----------------------------------------------------------------
> >This is through WebEtc Mail Server.....
--
----------------------------------------------------------------
FIROZ T M firoz@procsys.com
----------------------------------------------------------------
Technical Details
Message-ID:
<379C0465.4FCCC02B@procsys.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<379871C9.588B936C@procsys.com> <7na11v$oqt26@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,240329fa40c08b00 X-Google-Attributes: gid111d45,public From: Firoz T M <firoz@procsys.com> Subject: Re: Icons for each node in a Tree Date: 1999/07/26 Message-ID: <379C0465.4FCCC02B@procsys.com> X-Deja-AN: 505288817 Content-Transfer-Encoding: 7bit References: <379871C9.588B936C@procsys.com> <7na11v$oqt26@forums.borland.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing