用最简便的方法更换JTree图标[1]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

      最近为一个程序写界面,需要更换JTree的默认图标,因为以前从没做过swing,走了不少弯路,现在把方法告诉大家,希望对你的工作能有所帮助.

     先来一个常见的图标更换方法,需要显示继承DefaultTreeCellRenderer类,代码引用自http://blog.blogchina.com/174500.html

private class MyRenderer extends DefaultTreeCellRenderer {
           ImageIcon htmlIcon;
           ImageIcon imageIcon;
           public MyRenderer() {
               htmlIcon = new ImageIcon("source/html.gif");
               imageIcon=new ImageIcon("source/image.gif");
           }

           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 (leaf && isImage(value)) {
                   setIcon(imageIcon);
                   //setToolTipText("This book is in the Tutorial series.");
               }
               else if(leaf){
                   setIcon(htmlIcon);
               }

               return this;
           }

           protected boolean isImage(Object value) {
               Sitemarks.SitemarkEntry node =
                       (Sitemarks.SitemarkEntry)value;
               String name=
                       (String)(node.getName());

本文关键:用最简便的方法更换JTree图标
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top