Java GUI 三态导航树

1 通过Java Swing实现的一个三态树组件,包括选择、去选择、半选择等状态,适合网管等C/S结构的软件

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

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

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @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;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpzpw.html