import java.awt.*; import java.util.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.*; import javax.swing.tree.*;public class TestTree3 extends JFrame { JTree tree; DefaultTreeModel treeModel; public TestTree3( ) { super("Tree Test Example"); setSize(200, 150); setDefaultCloseOperation(EXIT_ON_CLOSE); // Add our own customized tree icons. UIManager.put("Tree.leafIcon", new ImageIcon("world.gif")); UIManager.put("Tree.openIcon", new ImageIcon("door.open.gif")); UIManager.put("Tree.closedIcon", new ImageIcon("door.closed.gif")); UIManager.put("Tree.expandedIcon", new ImageIcon("unlocked.gif")); UIManager.put("Tree.collapsedIcon", new ImageIcon("locked.gif")); } public void init( ) { // Build the hierarchy of containers and objects. String[] schoolyard = {"School", "Playground", "Parking Lot", "Field"}; String[] mainstreet = {"Grocery", "Shoe Shop", "Five & Dime", "Post Office"}; String[] highway = {"Gas Station", "Convenience Store"}; String[] housing = {"Victorian_blue", "Faux Colonial", "Victorian_white"}; String[] housing2 = {"Mission", "Ranch", "Condo"}; Hashtable homeHash = new Hashtable( ); homeHash.put("Residential 1", housing); homeHash.put("Residential 2", housing2); Hashtable cityHash = new Hashtable( ); cityHash.put("School grounds", schoolyard); cityHash.put("Downtown", mainstreet); cityHash.put("Highway", highway); cityHash.put("Housing", homeHash); Hashtable worldHash = new Hashtable( ); worldHash.put("My First VRML World", cityHash);
// Build our tree out of our big hashtable. tree = new JTree(worldHash); // Pick an angled line style. tree.putClientProperty("JTree.lineStyle", "Angled"); getContentPane( ).add(tree, BorderLayout.CENTER); } public static void main(String args[]) { TestTree3 tt = new TestTree3( ); tt.init( ); tt.setVisible(true); }}
import java.awt.*; import javax.swing.*; import javax.swing.tree.*;public class IconAndTipRenderer extends JLabel implements TreeCellRenderer { Color backColor = new Color(0xFF, 0xCC, 0xFF); Icon openIcon, closedIcon, leafIcon; String tipText = ""; public IconAndTipRenderer(Icon open, Icon closed, Icon leaf) { openIcon = open; closedIcon = closed; leafIcon = leaf; setBackground(backColor); setForeground(Color.black); } public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { setText(value.toString( )); if (selected) { setOpaque(true); } else { setOpaque(false); } // Try to find an IconAndTipCarrier version of the current node. IconAndTipCarrier itc = null; if (value instanceof DefaultMutableTreeNode) { Object uo = ((DefaultMutableTreeNode)value).getUserObject( ); if (uo instanceof IconAndTipCarrier) { itc = (IconAndTipCarrier)uo; } } else if (value instanceof IconAndTipCarrier) { itc = (IconAndTipCarrier)value; } if ((itc != null) && (itc.getIcon( ) != null)) { // Great! Use itc's values to customize this label. setIcon(itc.getIcon( )); tipText = itc.getToolTipText( ); } else { // Hmmm, nothing available, so rely on the defaults. tipText = " "; if (expanded) { setIcon(openIcon); } else if (leaf) { setIcon(leafIcon); } else { setIcon(closedIcon); } } return this; } // Override the default to send back different strings for folders and leaves. public String getToolTipText( ) { return tipText; } }
// TestTree3.java
// A simple test to see how we can build a tree and customize its icons
//
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.tree.*;public class TestTree3 extends JFrame { JTree tree;
DefaultTreeModel treeModel; public TestTree3( ) {
super("Tree Test Example");
setSize(200, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE); // Add our own customized tree icons.
UIManager.put("Tree.leafIcon", new ImageIcon("world.gif"));
UIManager.put("Tree.openIcon", new ImageIcon("door.open.gif"));
UIManager.put("Tree.closedIcon", new ImageIcon("door.closed.gif"));
UIManager.put("Tree.expandedIcon", new ImageIcon("unlocked.gif"));
UIManager.put("Tree.collapsedIcon", new ImageIcon("locked.gif"));
} public void init( ) {
// Build the hierarchy of containers and objects.
String[] schoolyard = {"School", "Playground", "Parking Lot", "Field"};
String[] mainstreet = {"Grocery", "Shoe Shop", "Five & Dime", "Post Office"};
String[] highway = {"Gas Station", "Convenience Store"};
String[] housing = {"Victorian_blue", "Faux Colonial", "Victorian_white"};
String[] housing2 = {"Mission", "Ranch", "Condo"};
Hashtable homeHash = new Hashtable( );
homeHash.put("Residential 1", housing);
homeHash.put("Residential 2", housing2); Hashtable cityHash = new Hashtable( );
cityHash.put("School grounds", schoolyard);
cityHash.put("Downtown", mainstreet);
cityHash.put("Highway", highway);
cityHash.put("Housing", homeHash); Hashtable worldHash = new Hashtable( );
worldHash.put("My First VRML World", cityHash);
// Build our tree out of our big hashtable.
tree = new JTree(worldHash); // Pick an angled line style.
tree.putClientProperty("JTree.lineStyle", "Angled");
getContentPane( ).add(tree, BorderLayout.CENTER);
} public static void main(String args[]) {
TestTree3 tt = new TestTree3( );
tt.init( );
tt.setVisible(true);
}}
// IconAndTipRenderer.java
// A renderer for our XML cells
//
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;public class IconAndTipRenderer extends JLabel implements TreeCellRenderer { Color backColor = new Color(0xFF, 0xCC, 0xFF);
Icon openIcon, closedIcon, leafIcon;
String tipText = ""; public IconAndTipRenderer(Icon open, Icon closed, Icon leaf) {
openIcon = open;
closedIcon = closed;
leafIcon = leaf;
setBackground(backColor);
setForeground(Color.black);
} public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected,
boolean expanded, boolean leaf,
int row, boolean hasFocus) {
setText(value.toString( ));
if (selected) {
setOpaque(true);
}
else {
setOpaque(false);
} // Try to find an IconAndTipCarrier version of the current node.
IconAndTipCarrier itc = null;
if (value instanceof DefaultMutableTreeNode) {
Object uo = ((DefaultMutableTreeNode)value).getUserObject( );
if (uo instanceof IconAndTipCarrier) {
itc = (IconAndTipCarrier)uo;
}
}
else if (value instanceof IconAndTipCarrier) {
itc = (IconAndTipCarrier)value;
}
if ((itc != null) && (itc.getIcon( ) != null)) {
// Great! Use itc's values to customize this label.
setIcon(itc.getIcon( ));
tipText = itc.getToolTipText( );
}
else {
// Hmmm, nothing available, so rely on the defaults.
tipText = " ";
if (expanded) {
setIcon(openIcon);
}
else if (leaf) {
setIcon(leafIcon);
}
else {
setIcon(closedIcon);
}
}
return this;
} // Override the default to send back different strings for folders and leaves.
public String getToolTipText( ) {
return tipText;
}
}希望楼主给我加分哦。我还没有得过分呢,鼓励一下嘛,呵呵
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.tree.*;public class TestTree3 extends JFrame { JTree tree;
DefaultTreeModel treeModel; public TestTree3( ) {
super("Tree Test Example");
setSize(200, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE); // Add our own customized tree icons.
UIManager.put("Tree.leafIcon", new ImageIcon("world.gif"));
UIManager.put("Tree.openIcon", new ImageIcon("door.open.gif"));
UIManager.put("Tree.closedIcon", new ImageIcon("door.closed.gif"));
UIManager.put("Tree.expandedIcon", new ImageIcon("unlocked.gif"));
UIManager.put("Tree.collapsedIcon", new ImageIcon("locked.gif"));
} public void init( ) {
// Build the hierarchy of containers and objects.
String[] schoolyard = {"School", "Playground", "Parking Lot", "Field"};
String[] mainstreet = {"Grocery", "Shoe Shop", "Five & Dime", "Post Office"};
String[] highway = {"Gas Station", "Convenience Store"};
String[] housing = {"Victorian_blue", "Faux Colonial", "Victorian_white"};
String[] housing2 = {"Mission", "Ranch", "Condo"};
Hashtable homeHash = new Hashtable( );
homeHash.put("Residential 1", housing);
homeHash.put("Residential 2", housing2); Hashtable cityHash = new Hashtable( );
cityHash.put("School grounds", schoolyard);
cityHash.put("Downtown", mainstreet);
cityHash.put("Highway", highway);
cityHash.put("Housing", homeHash); Hashtable worldHash = new Hashtable( );
worldHash.put("My First VRML World", cityHash);
// Build our tree out of our big hashtable.
tree = new JTree(worldHash); // Pick an angled line style.
tree.putClientProperty("JTree.lineStyle", "Angled");
getContentPane( ).add(tree, BorderLayout.CENTER);
} public static void main(String args[]) {
TestTree3 tt = new TestTree3( );
tt.init( );
tt.setVisible(true);
}}
import javax.swing.*;
import javax.swing.tree.*;public class IconAndTipRenderer extends JLabel implements TreeCellRenderer { Color backColor = new Color(0xFF, 0xCC, 0xFF);
Icon openIcon, closedIcon, leafIcon;
String tipText = ""; public IconAndTipRenderer(Icon open, Icon closed, Icon leaf) {
openIcon = open;
closedIcon = closed;
leafIcon = leaf;
setBackground(backColor);
setForeground(Color.black);
} public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected,
boolean expanded, boolean leaf,
int row, boolean hasFocus) {
setText(value.toString( ));
if (selected) {
setOpaque(true);
}
else {
setOpaque(false);
} // Try to find an IconAndTipCarrier version of the current node.
IconAndTipCarrier itc = null;
if (value instanceof DefaultMutableTreeNode) {
Object uo = ((DefaultMutableTreeNode)value).getUserObject( );
if (uo instanceof IconAndTipCarrier) {
itc = (IconAndTipCarrier)uo;
}
}
else if (value instanceof IconAndTipCarrier) {
itc = (IconAndTipCarrier)value;
}
if ((itc != null) && (itc.getIcon( ) != null)) {
// Great! Use itc's values to customize this label.
setIcon(itc.getIcon( ));
tipText = itc.getToolTipText( );
}
else {
// Hmmm, nothing available, so rely on the defaults.
tipText = " ";
if (expanded) {
setIcon(openIcon);
}
else if (leaf) {
setIcon(leafIcon);
}
else {
setIcon(closedIcon);
}
}
return this;
} // Override the default to send back different strings for folders and leaves.
public String getToolTipText( ) {
return tipText;
}
}
{
//这些你要男或女的图片对象
Icon localIcon; Icon inBoxIcon; public SmsTreeRenderer(Icon localIcon, Icon inBoxIcon)
{
this.localIcon = localIcon;
this.inBoxIcon = inBoxIcon;
} 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);
if (这里判断是否为男)
{
//加男图片
setIcon(localIcon);
} else {
//加女图片
setIcon(inBoxIcon);
} return this;
}}
具体使用:
创建树对象,设置树对象方法smsTree.setCellRenderer(new SmsTreeRenderer(new ImageIcon("男图片"), new ImageIcon("女图片"));