如何在树形表中添加复选框 [英] How can I add checkbox in tree table

查看:103
本文介绍了如何在树形表中添加复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用多列创建一个Tree.我在此处(德语)中找到了该教程,并在?

I want to created a Tree with multi-columns. I found this tutorial here (German) and this answer (English). I want to add checkboxes in one column, but I have no idea how to do it. When I return a checkbox to JTreeTable, something show in execute is checkbox detail not checkbox object. How can I get something like this, pictured below?

推荐答案

以新的摇摆树表进行旋转 ,在此处引用,您的

As shown in Taking the New Swing Tree Table for a Spin, cited here, your implementation of RowModel must return the correct type from getColumnClass() and the correct value from getValueFor(). Values of type Boolean.class will be rendered with a JCheckBox. The following implementations produce the image cited:

@Override
public Class getColumnClass(int column) {
    switch (column) {
        case 0:
            return Date.class;
        case 1:
            return Long.class;
        case 2:
            return Boolean.class;
        case 3:
            return Boolean.class;
        case 4:
            return Boolean.class;
        default:
            assert false;
    }
    return null;
}

@Override
public Object getValueFor(Object node, int column) {
    File f = (File) node;
    switch (column) {
        case 0:
            return new Date(f.lastModified());
        case 1:
            return f.length();
        case 2:
            return f.canRead();
        case 3:
            return f.canWrite();
        case 4:
            return f.canExecute();
        default:
            assert false;
    }
    return null;
}

我无法选择复选框.

I can't select checkbox.

您需要在isCellEditable()的实现中为所需的列返回true,并在setValueFor()的实现中相应地更新node.单元格编辑器结束时,将调用您的setValueFor()实现,因此请验证其是否更新了 same 值,该值稍后将由getValueFor()返回. (可选)您将要使用 DefaultTreeModel 代码是一个很好的例子.

You need to return true in your implementation of isCellEditable() for the desired column(s) and update the node in your implementation of setValueFor() accordingly. When the cell editor concludes, your implementation of setValueFor() will be called, so verify that it updates the same value that will later be returned by getValueFor(). Optionally, you'll want to implement the TreeModel methods that manage the TreeModelListener list by using the scheme prescribed in an EventListenerList API; the DefaultTreeModel source code is a good example.

这篇关于如何在树形表中添加复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆