当前位置: 技术问答>java相关
如何为JTree设置不同的节点图标?
来源: 互联网 发布时间:2015-04-18
本文导语: 通过DefaultTreeCellRenderer可以设置枝或叶两种图标,但是我现在是想要为不同的枝设置不同的图标,比如 Server - Database - Table 三级,我希望是不同的图标,这样如何实现? | 将JLabel当作节点或树叶加...
通过DefaultTreeCellRenderer可以设置枝或叶两种图标,但是我现在是想要为不同的枝设置不同的图标,比如 Server - Database - Table 三级,我希望是不同的图标,这样如何实现?
|
将JLabel当作节点或树叶加入。。。。。。。。。
|
是的,扩展JLabel实现TreeCellRenderer,然后作为DefaultMutableTreeNode的参数就可以了。如果扩展别的组件就可以实现更复杂的功能,呵呵。
|
我的代码
package untitled5;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Frame1 extends JFrame
{
JPanel contentPane;
ImageIcon imageicona = new ImageIcon("10-1.jpg");
ImageIcon imageiconb = new ImageIcon("10-2.jpg");
ImageIcon imageiconc = new ImageIcon("10-3.jpg");
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JScrollPane jScrollPane1 = new JScrollPane();
BorderLayout borderLayout2 = new BorderLayout();
JLabel jLabel1 = new JLabel("A",imageicona,JLabel.LEFT);
JLabel jLabel2 = new JLabel("B",imageiconb,JLabel.LEFT);
JLabel jLabel3 = new JLabel("C",imageiconc,JLabel.LEFT);
//JLabel [][] j = {{jLabel1,jLabel2,jLabel3},{jLabel2}};
JTree jTree1 = new JTree(getD());
//Construct the frame
public Frame1()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception
{
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jPanel2.setLayout(borderLayout2);
/*
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel2",imageiconb,JLabel.LEFT);
jLabel3.setText("jLabel3",imageiconc,JLabel.LEFT);
*/
contentPane.add(jPanel1, BorderLayout.NORTH);
jPanel1.add(jLabel3, null);
jPanel1.add(jLabel2, null);
jPanel1.add(jLabel1, null);
contentPane.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTree1, null);
MyDefaultTreeCellRenderer renderer = new MyDefaultTreeCellRenderer();
// renderer.setLeafIcon(imageiconc);
jTree1.setCellRenderer(renderer);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
private TreeModel getD()
{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("A");
DefaultMutableTreeNode p,q;
p = new DefaultMutableTreeNode("B");
q = new DefaultMutableTreeNode("C");
q.add(new DefaultMutableTreeNode("D"));
root.add(p);
root.add(new DefaultMutableTreeNode("X"));
p.add(q);
return new DefaultTreeModel(root);
}
}
package untitled5;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.Component;
import javax.swing.tree.DefaultTreeModel;
public class MyDefaultTreeCellRenderer extends DefaultTreeCellRenderer
{
public MyDefaultTreeCellRenderer()
{
}
public static void main(String[] args)
{
MyDefaultTreeCellRenderer myDefaultTreeCellRenderer1 = new MyDefaultTreeCellRenderer();
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
{
super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
// node.setIcon(new ImageIcon("10-1.jpg"));
/*
String tmp = node.toString();
System.out.println(row);
if (tmp.equals("A"))
this.setIcon(new ImageIcon("10-1.jpg"));
else if (tmp.equals("B"))
this.setIcon(new ImageIcon("10-2.jpg"));
else if (tmp.equals("C"))
this.setIcon(new ImageIcon("10-3.jpg"));
else if (tmp.equals("D"))
this.setIcon(new ImageIcon("10-1.jpg"));
*/
switch(node.getLevel() )
{
case 0 : this.setIcon(new ImageIcon("10-1.jpg"));
break;
case 1 : this.setIcon(new ImageIcon("10-2.jpg"));
break;
case 2 : this.setIcon(new ImageIcon("10-3.jpg"));
break;
}
return this;
}
}
可以实现你的要求。。。不是到是不是最好的方法。。。
你的想法呢??
package untitled5;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Frame1 extends JFrame
{
JPanel contentPane;
ImageIcon imageicona = new ImageIcon("10-1.jpg");
ImageIcon imageiconb = new ImageIcon("10-2.jpg");
ImageIcon imageiconc = new ImageIcon("10-3.jpg");
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JScrollPane jScrollPane1 = new JScrollPane();
BorderLayout borderLayout2 = new BorderLayout();
JLabel jLabel1 = new JLabel("A",imageicona,JLabel.LEFT);
JLabel jLabel2 = new JLabel("B",imageiconb,JLabel.LEFT);
JLabel jLabel3 = new JLabel("C",imageiconc,JLabel.LEFT);
//JLabel [][] j = {{jLabel1,jLabel2,jLabel3},{jLabel2}};
JTree jTree1 = new JTree(getD());
//Construct the frame
public Frame1()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception
{
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jPanel2.setLayout(borderLayout2);
/*
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel2",imageiconb,JLabel.LEFT);
jLabel3.setText("jLabel3",imageiconc,JLabel.LEFT);
*/
contentPane.add(jPanel1, BorderLayout.NORTH);
jPanel1.add(jLabel3, null);
jPanel1.add(jLabel2, null);
jPanel1.add(jLabel1, null);
contentPane.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTree1, null);
MyDefaultTreeCellRenderer renderer = new MyDefaultTreeCellRenderer();
// renderer.setLeafIcon(imageiconc);
jTree1.setCellRenderer(renderer);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
private TreeModel getD()
{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("A");
DefaultMutableTreeNode p,q;
p = new DefaultMutableTreeNode("B");
q = new DefaultMutableTreeNode("C");
q.add(new DefaultMutableTreeNode("D"));
root.add(p);
root.add(new DefaultMutableTreeNode("X"));
p.add(q);
return new DefaultTreeModel(root);
}
}
package untitled5;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.Component;
import javax.swing.tree.DefaultTreeModel;
public class MyDefaultTreeCellRenderer extends DefaultTreeCellRenderer
{
public MyDefaultTreeCellRenderer()
{
}
public static void main(String[] args)
{
MyDefaultTreeCellRenderer myDefaultTreeCellRenderer1 = new MyDefaultTreeCellRenderer();
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
{
super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
// node.setIcon(new ImageIcon("10-1.jpg"));
/*
String tmp = node.toString();
System.out.println(row);
if (tmp.equals("A"))
this.setIcon(new ImageIcon("10-1.jpg"));
else if (tmp.equals("B"))
this.setIcon(new ImageIcon("10-2.jpg"));
else if (tmp.equals("C"))
this.setIcon(new ImageIcon("10-3.jpg"));
else if (tmp.equals("D"))
this.setIcon(new ImageIcon("10-1.jpg"));
*/
switch(node.getLevel() )
{
case 0 : this.setIcon(new ImageIcon("10-1.jpg"));
break;
case 1 : this.setIcon(new ImageIcon("10-2.jpg"));
break;
case 2 : this.setIcon(new ImageIcon("10-3.jpg"));
break;
}
return this;
}
}
可以实现你的要求。。。不是到是不是最好的方法。。。
你的想法呢??