Java GUI 三态导航树
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;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @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);
}
}
评论暂时关闭