设为首页 加入收藏

TOP

Java GUI 三态导航树(一)
2014-11-24 02:41:06 来源: 作者: 【 】 浏览:3
Tags:Java GUI 导航

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(getBackgroundNonSelectionCol

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇递归实现汉诺塔(JAVA语言) 下一篇Java时间和本地Linux 时间不一致..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: