问题描述
我想用多列创建一个Tree
.我在此处(德语)中找到了该教程,并在(英语).我想在一栏中添加复选框,但是我不知道该怎么做.当我将复选框返回到JTreeTable
时,execute中显示的内容是复选框详细信息而不是复选框对象.我如何获得如下图所示的此?
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?
推荐答案
如 以新的摇摆树表进行旋转 ,在此处引用,您的 RowModel
必须从getColumnClass()
返回正确的类型,并从getValueFor()
返回正确的值.类型为Boolean.class
的值将使用JCheckBox
呈现.以下实现产生引用的图像:
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;
}
您需要在isCellEditable()
的实现中为所需的列返回true
,并在setValueFor()
的实现中相应地更新node
.单元格编辑器结束时,将调用您的setValueFor()
实现,因此请验证其是否更新了 same 值,该值稍后将由getValueFor()
返回. (可选)您将要使用 EventListenerList
API; 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.
这篇关于如何在树形表中添加复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!