java三态_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(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;

}

}

return super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

}

}0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值