swing标准库,实在有些简陋,很多显然的需求却无法满足,jdk5以前的版本,连表格列的排序功能都没有,swing最大的毛病也在这。最近学习python,同时也学习了一些GUI库,发现基本上tree都支持多列显示,在swing里,你就必须找第三方库了。
废话不说了,还是说说本来的需求。
我是需要在tree的node上显示checkbox。要满足这个需求,最明显的就有两个方法。
1 实现TreeCellRenderer接口,JCheckBox来做渲染,这样就可以,不过这有个问题,就是感观上和默认node实现,有很多差异,一些如选中,焦点,背景色等等,这些在DefaultTreeCellRenderer之类里有很多关于此类的考虑,如果自己再考虑,写一遍,似乎不合适。
2 用icon图片来代替,这是个方法,但是我有个期望是,希望checkbox能跟随look&feel做切换,所以期望是系统采用各个look&feel自己的chekbox。
大家知道,swing是自己画的,这样的话,那么icon也是,也就是说,我可以借用JCheckBox的渲染来渲染treenode的icon。最初想法是,直接用JCheckBox的icon放到DefaultTreeCellRenderer的icon里,但却报错,原因是DefaultTreeCellRenderer父类是JLabel,每个组件的icon的渲染,与组件相关的model是紧密联系的。
void paintIcon(Component c, Graphics g, int x, int y);
不同组件的icon不能直接使用。那么怎么办呢?其实也很简单,保证paintIcon的Component参数与icon一致就行了。
具体代码:
public class CheckboxTreeNodeRenderer extends DefaultTreeCellRenderer implements
TreeCellRenderer {
private static final long serialVersionUID = 1L;
private JCheckBox checkbox = new JCheckBox();
private Icon checkboxIcon = UIManager.getIcon("CheckBox.icon");
private ProxyCheckBoxIcon proxyIcon = new ProxyCheckBoxIcon();
private Font normal;
private Font disabled;
@SuppressWarnings("unchecked")
public CheckboxTreeNodeRenderer() {
normal = getFont().deriveFont(Font.PLAIN);
Map<TextAttribute, Object> attrs = (Map<TextAttribute, Object>) normal.getAttributes();
attrs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
//attrs.put(TextAttribute.FONT, getFont().deriveFont(Font.ITALIC));
disabled = getFont().deriveFont(attrs);
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded,
leaf, row, hasFocus);
if (value instanceof CheckboxTreeNode) {
CheckboxTreeNode ctn = (CheckboxTreeNode) value;
checkbox.setSelected(ctn.isChecked());
setIcon(proxyIcon);
if (ctn.isChecked()) {
setFont(disabled);
if (!selected)
setForeground(Color.GRAY);
} else {
setFont(normal);
//setForeground(Color.BLACK);
}
} else {
setFont(normal);
}
return this;
}
private class ProxyCheckBoxIcon implements Icon, Serializable {
private static final long serialVersionUID = 1L;
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
checkboxIcon.paintIcon(checkbox, g, x, y);
}
@Override
public int getIconWidth() {
return checkboxIcon.getIconWidth();
}
@Override
public int getIconHeight() {
return checkboxIcon.getIconHeight();
}
}
}
以上代码除了实现checbox外,还实现了选中时,把显示文字增加删除线。(反正没找到简单方法,但在QT里很简单)。
下面是选中节点的判断监听类:
public class CheckboxNodeCheckedListener extends MouseAdapter implements MouseListener {
@Override
public void mousePressed(MouseEvent e) {
JTree tree = (JTree) e.getSource();
TreePath clickedPath = tree.getPathForLocation(e.getX(), e.getY());
if (e.getClickCount() == 1) {
if (clickedPath != null && !tree.isEditing()) {
Object node = clickedPath.getLastPathComponent();
if (node instanceof CheckboxTreeNode) {
CheckboxTreeNode treeNode = (CheckboxTreeNode) node;
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
TreeNode[] currPath = model.getPathToRoot(treeNode);
Rectangle rec = tree.getPathBounds(new TreePath(currPath));
if (e.getX() - rec.getX() <= 13) { // 默认为 13x13大小
treeNode.setChecked(!treeNode.isChecked());
model.nodeChanged(treeNode);
}
}
}
}
}
}
分享到:
相关推荐
带checkbox的树 checkbox tree tree tree checkbox
//解决的问题是Tree控件的checkbox不能设置为不允许用户勾选 //设置一个标志位,在整棵树加载完成后就将标志位设置为false,不允许勾选checkbox了,在加载数据完成之前是可以勾选的,用来在数据中设置勾选了哪些节点 /...
本资源中包括Tree的文件和Tree的渲染文件(Checkbox),实现功能为点击树不获取值(也可以获取,改变一个变量即可),只有勾选Checkbox才能得到。并添加到集合中
界面精美的javascript树,方便简洁!
flex tree+checkbox可实现级联勾选
ExtJs4 Checkbox tree
带CheckBox和级联操作的Tree 带CheckBox和级联操作的Tree 带CheckBox和级联操作的Tree
Tree checkbox Demo
TreeCheckBox 数据源ArrayCollication 数据源返回的是 state用意判断状态
在网上找到了关于flex 的tree在前面加上checkbox, 我经过改动, 现在很适合使用。
EXTJS4.2 Sencha Architect22 treegrid tree checkbox treegrid tree 通过store显示内容,能够通过checkbox点击勾选 其他功能,网上都能找到,就没有再写
selectTree tree控件 日历控件 tree控件 radio CheckBox demo 直接可以用 案例多多
ligerUI Tree对checkbox的禁用,设置成只读,只要早jsp中引入此文件即可。
flex tree中渲染checkbox
一个有复选框的权限树.可以提取权限
NULL 博文链接:https://danferj.iteye.com/blog/525889
根据Z-Tree进行的扩展,有详细的使用例子,example.txt
JTree(节点带有checkbox)代码实例
项目中需要实现Header与Node的CheckBox联动,在VirtualStringTree的Demo中没有发现相关的例子,网友提供的众多例子中也未涉及这方面的内容。代码只有短短几行,现上传供有同样需求的朋友参考。
flex中tree和checkbox结合使用的插件,该压缩包包含两个文档,一个是tree和checkbox结合使用的插件,另一个是使用示例