Java GUI 三态导航树

本网站用的阿里云ECS,推荐大家用。自己搞个学习研究也不错
1 通过Java Swing实现的一个三态树组件,包括选择、去选择、半选择等状态,适合网管等C/S结构的软件

2 代码是整合完善多个版本/多位大侠的工作后,输出的一个较为满意的版本,还有很多值得优化的空间,欢迎大家修改完善。

3 大家在使用时放到一个包里即可运行, 三态树的节点为任意的Object,可以根据具体情况设置对象类型,可以很好的进行多态等处理, 代码简洁, Cell可以设置属性,可以根据具体情况扩展

/**
 *

Title:

 *
 *

Description:

 *
 *

Copyright: Copyright (c) 2007

 *
 *

Company:

 *
 * @author not attributable
 * @version 1.0
 */
public class FilterTreeCell
    implements Cloneable
{
    public static final int NONE_SELECT = 0;
    public static final int ALL_SELECT = 1;
    public static final int PART_SELECT = 2;

    int iState;
    Object filterObject;
    private String attribute = null;

    public FilterTreeCell(Object filterObject, int state)
    {
        iState = state;
        this.filterObject = filterObject;
    }

    public FilterTreeCell(Object filterObject)
    {
        this(filterObject, NONE_SELECT);
    }

    public FilterTreeCell()
    {
        this(new String(), NONE_SELECT);
    }

    public Object getFilterObject()
    {
        return filterObject;
    }

    public void setFilterObject(Object filterObject)
    {
        this.filterObject = filterObject;
    }

    public String toString()
    {
        return filterObject.toString();
    }

    public void changeState()
    {
        if (iState == PART_SELECT || iState == ALL_SELECT)
        {
            changeState(NONE_SELECT);
        }
        else if (NONE_SELECT == iState)
        {
            changeState(ALL_SELECT);
        }
    }

    public void changeState(int state)
    {
        if (iState == state)
        {
            return;
        }
        else
        {
            iState = state;
            return;
        }
    }

    public int getState()
    {
        return iState;
    }

    public Object clone()
    {
        FilterTreeCell newCell = null;

        try
        {
            newCell = (FilterTreeCell)super.clone();
            newCell.changeState(getState());
        }
        catch (Exception e)
        {}

        return newCell;
    }

    public boolean equals(Object obj)
    {
        if (obj instanceof FilterTreeCell)
        {
            return filterObject.equals( ( (FilterTreeCell) obj).filterObject);
        }
        else
        {
            return false;
        }
    }
   
    /**
  * @return Returns the attribute.
  */
 public String getAttribute()
 {
  return attribute;
 }

 /**
  * @param attribute The attribute to set.
  */
 public void setAttribute(String attribute)
 {
  this.attribute = attribute;
 }
}

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *

Title:

 *
 *

Description:

 *
 *

Copyright: Copyright (c) 2007

 *
 *

Company:

 *
 * @author not attributable
 * @version 1.0
 */

public class FilterTreeCellRenderer
    extends DefaultTreeCellRenderer
{
    private JLabel label = new JLabel();
    private JPanel panel = new JPanel();
    private JCheckBox checkBox = new JCheckBox();
    private Icon allSelectIcon = new ImageIcon(getClass().getResource(“all.gif”));
    private Icon partSelectIcon = new ImageIcon(getClass().getResource(“part.gif”));
    private Icon noneSelectIcon = new ImageIcon(getClass().getResource(“none.gif”));

    public FilterTreeCellRenderer()
    {
        setOpenIcon(null);
        setClosedIcon(null);
        setLeafIcon(null);

        panel.setLayout(new FlowLayout(0, 0, 0));
        panel.setBackground(getBackgroundNonSelectionColor());
        checkBox.setPreferredSize(new Dimension(18, 18));
        checkBox.setBorderPainted(false);
        checkBox.setBackground(getBackgroundNonSelectionColor());
        label.setBackground(getBackgroundNonSelectionColor());
        panel.setBackground(getBackgroundNonSelectionColor());
        panel.add(checkBox);
        panel.add(label);
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
                                                  boolean leaf, int row, boolean hasFocus)
    {
        if (value instanceof DefaultMutableTreeNode)
        {
            Object obj = ( (DefaultMutableTreeNode) value).getUserObject();
            if (obj instanceof FilterTreeCell)
            {
                FilterTreeCell cell = (FilterTreeCell) obj;
                label.setText(cell.getFilterObject().toString());
                int state = cell.getState();

                switch (state)
                {
                    case FilterTreeCell.NONE_SELECT:
                        checkBox.setSelected(false);
                        checkBox.setIcon(noneSelectIcon);
                        break;

                    case FilterTreeCell.PART_SELECT:
                        checkBox.setSelected(false);
                        checkBox.setIcon(partSelectIcon);
                        break;

                    case FilterTreeCell.ALL_SELECT:
                        checkBox.setSelected(true);
                        checkBox.setIcon(allSelectIcon);
                        break;
                }

                label.setEnabled(tree.isEnabled());

                if (selected)
                {
                    panel.setBackground(getBackgroundSelectionColor());
                    label.setForeground(getTextSelectionColor());
                }
                else
                {
                    panel.setBackground(getBackgroundNonSelectionColor());
                    label.setForeground(getTextNonSelectionColor());
                }

                panel.setComponentOrientation(tree.getComponentOrientation());

                return panel;
           

未经允许不得转载:演道网 » Java GUI 三态导航树

赞 (0)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册