使用JCheckBox帮助创建JTree [英] Help making a JTree with a JCheckBox

查看:168
本文介绍了使用JCheckBox帮助创建JTree的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不寻常的情况,我需要有一个JTree每个节点包含2个复选框和一个标签(有能力添加一个监听器来告诉什么时候选中任何潜在的复选框)。我还需要根节点具有相同的布局(我假设意味着创建一个JPanel与2 JCheckBoxes和一个JLabel),能够选择树中的所有复选框,如果在根中选中一个。 p>

任何指导或示例?我已经检查了以前的问题在这里和相关的例子...其中一些允许我到树的看点,但没有给我一个方向来实现它背后的行动。



谢谢!

解决方案

这可能是一个很好的时间来看看老 JTreeTable 代码,它将为您提供在第一列,并且可以自由地根据需要将每个列的单元格呈现在树节点的右侧,在您的情况下,放入复选框和一个标签,并允许您使用TableCellEditor与您以前的JTable一起工作。一个警告是,虽然该链接中的代码工作,它是一个有点复杂。



有另一种选择。我已经在下面演示了一个应该更好的Tree Table实现,称为NetBeans提供的 Outline (尽管您不需要使用NetBeans IDE开发,需要jar)。 本文说明了如何轻松开始。



我可以在Eclipse中创建一个Outline树表的快速示例(将org-netbeans-swing-outline.jar导入到我的项目中) 30分钟(我输入时很慢):

  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);
//这里是netBeans树表类
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));

}

我创建一个TreeNode来保存 Boolean ,它将与 JTable 的内置复选框渲染mechnanism互操作。

  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;}

}



netBeans RowModel是一个表,而不是一个简单的JTree的关键:

  public class MyRowModel implements RowModel {

public class getColumnClass(int col){
switch(col){
case 0:return String。类;
case 1:return Boolean.class; //这些返回类定义将
case 2:return Boolean.class; //触发复选框渲染
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);}
//编辑:这里是一个递归方法设置所有的孩子
//为两个复选框之一选中,因为它是
//由父
检查(Enumeration children = n .children();
children.hasMoreElements();){
MyTreeNode child =(MyTreeNode)children.nextElement
setValueFor(child,col,val);
}


}

}


b $ b

这里是完成的,虽然是简单的产品:





我更新了 setValueFor 子项,并将复选框设置为已选中或在父项已修改时取消选中。


I have an unusual situation where I need to have a JTree with each node containing 2 checkboxes and a label (with the ability to add a listener to tell when any of the potential checkboxes are checked). I also need the root node to have the same layout (which I'm assuming means creating a JPanel with 2 JCheckBoxes and a JLabel), with the ability to select all the checkboxes down the tree if one in the root is checked.

Any guidance or examples? I've checked out previous questions on here and associated examples...some of which allowed me to get to the point of having the tree "look" but without giving me a direction for implementing the action behind it.

Thanks!

解决方案

This might be a good time to look at the old JTreeTable code, which will give you a tree rendered in the first column, and the freedom to render the cells for each column to the right of the tree node as you wish, in your case putting in checkboxes and a label, and allowing you to have TableCellEditors working with your JTable as you are used to. A warning is that, while the code in that link works, it is a little convoluted.

There is an alternative. I have demoed below a Tree Table implementation that is supposed to be better, called Outline, provided by NetBeans (though you don't need to develop with the NetBeans IDE, you just need the jar). This article indicates how easy it is to be to get started.

I was able to mock up a quick example of the Outline tree table in Eclipse (with the org-netbeans-swing-outline.jar imported to my project) in about 30 minutes (I am slow at typing):

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"));

}

I create a TreeNode to hold the Booleans that will interoperate well with the JTable's built-in checkbox rendering mechnanism.

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;}

}

The netBeans RowModel is the key to making this a table instead of a simple 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);
        }


    }

}

here is the finished, albeit simplistic, product:

I have updated the setValueFor method to iterate over a node's children and set the checkboxes as selected or deselected when a parent has been modified.

这篇关于使用JCheckBox帮助创建JTree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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