这可能是查看旧JTreeTable代码的好时机,它将为您提供在第一列中呈现的树,并且可以根据需要自由地将每个列的单元格呈现在树节点的右侧,在您的情况下放入复选框和标签,并允许您像以前一样让TableCellEditors与JTable一起使用.一个警告是,虽然该链接中的代码有效,但它有点复杂.
还有另一种选择.我在下面演示了一个更好的树表实现,称为OutlineNetBeans提供的实现(虽然您不需要使用NetBeans IDE进行开发,但您只需要jar). 本文指出了入门是多么容易.
我能够在大约30分钟内模拟Eclipse中大纲树表的快速示例(将org-netbeans-swing-outline.jar导入到我的项目中)(我输入速度慢):
private void buildFrame() {
frame = new JFrame("Demo");
frame.setSize(300, 300);
addStuffToFrame();
frame.setVisible(true);
}
private void addStuffToFrame() {
MyTreeNode top = new MyTreeNode("top");
createNodes(top);
DefaultTreeModel model = new DefaultTreeModel(top);
//here are the netBeans tree table classes
OutlineModel outlineModel =
DefaultOutlineModel.createOutlineModel(model, new MyRowModel());
Outline outline = new Outline();
outline.setRootVisible(true);
outline.setModel(outlineModel);
frame.getContentPane().add(new JScrollPane(outline));
}
private void createNodes(MyTreeNode top) {
MyTreeNode child = new MyTreeNode("child 2");
top.add(new MyTreeNode("child 1"));
child.add(new MyTreeNode("g-child1"));
child.add(new MyTreeNode("g-child2"));
child.add(new MyTreeNode("g-child3"));
top.add(child);
top.add(new MyTreeNode("child3"));
top.add(new MyTreeNode("child4"));
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个TreeNode来保存Boolean与JTable内置复选框渲染机制完全互操作的s.
public class MyTreeNode extends DefaultMutableTreeNode {
Boolean data1 = null;
Boolean data2 = null;
String name = null;
MyTreeNode (String name) {
this.name=name;
}
void setData1(Boolean val) {data1=val;}
void setData2(Boolean val) {data2=val;}
Boolean getData1() {return data1;}
Boolean getData2() {return data2;}
String getName() {return name;}
}
Run Code Online (Sandbox Code Playgroud)
netBeans RowModel是使其成为表而不是简单JTree的关键:
public class MyRowModel implements RowModel {
public Class getColumnClass(int col) {
switch (col) {
case 0: return String.class;
case 1: return Boolean.class; //these return class definitions will
case 2: return Boolean.class; //trigger the checkbox rendering
default:return null;
}
}
public int getColumnCount() {
return 3;
}
public String getColumnName(int col) {
return "";
}
public Object getValueFor(Object node, int col) {
MyTreeNode n = (MyTreeNode)node;
switch (col) {
case 0: return n.getName();
case 1: return n.getData1();
case 2: return n.getData2();
default:return null;
}
}
public boolean isCellEditable(Object node, int col) {
return col > 0;
}
public void setValueFor(Object node, int col, Object val) {
MyTreeNode n = (MyTreeNode)node;
if (col == 1) {n.setData1((Boolean)val);}
else if (col == 2) {n.setData2((Boolean)val);}
//EDIT: here is a recursive method to set all children
// selected for one of the two checkboxes as it is
// checked by the parent
for (Enumeration children = n.children();
children.hasMoreElements(); ) {
MyTreeNode child = (MyTreeNode) children.nextElement();
setValueFor(child, col, val);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是完成的,虽然简单的产品:
替代文字http://img17.imageshack.us/img17/6643/picture1hz.png
我更新了setValueFor迭代节点子节点的方法,并在修改父节点时将复选框设置为选中或取消选中.