TreeView节点命名为bugging

时间:2016-12-06 14:24:43

标签: java treeview defaultmutabletreenode

我在使用TreeView时遇到了很多麻烦,更具体地说明了每个节点显示的文本。

从树的初始化程序开始,我希望单个节点带有文本,这可能会使程序对用户更直观:

private void defineEmpityTree(){            
    cbt.setModel(new DefaultTreeModel(
        new DefaultMutableTreeNode("Relat\u00F3rios Individuais") {
            {
            }
        }
    ));
}

我添加初始化树:

cbt = new JCheckBoxTree();
    defineEmpityTree();
    scrollPane.setViewportView(cbt);

"Relatórios Individuais" aren't shown, Tree Ok, another bug

我忽略了这个问题并继续实际填充节点,用户指定他的搜索并按下" ok",当我们得到树填充的第二张图像时。

然后又出现了另一个奇怪的问题,如果" ok"再次按下,有些文字变得奇怪,第3张图片。

继承人创建节点的部分。对于为每种类型的报告和每个报告批次创建的文件数组,它并不重要。

private void defineNewTree(ArrayList<File> files){
    DefaultMutableTreeNode dmtRoot = new DefaultMutableTreeNode("Relat\u00F3rios Individuais");
    DefaultMutableTreeNode dmtSubFolder = null;
    DefaultMutableTreeNode dmtLotFolder = null;
    int childsRoot = 0;
    int childsSub = 0;

    for(File f : files){
        String subFolder = f.getName().substring(17,f.getName().length()-4);
        String name = f.getName();
        String lot = f.getName().substring(0, 3);
        childsRoot = dmtRoot.getChildCount();
        boolean subFoldExists = false;
        boolean foldLotExists = false;
        //creatingo folder reports:
        for(int i = 0;i<childsRoot;i++){
            if(dmtRoot.getChildAt(i).toString().equals(subFolder)){
                dmtSubFolder = (DefaultMutableTreeNode) dmtRoot.getChildAt(i);          
                subFoldExists = true;
                i=childsRoot;
            }
        }
        if(!subFoldExists){ 
            dmtSubFolder = new DefaultMutableTreeNode(subFolder);
            dmtRoot.add(dmtSubFolder);
        }
        for(int j = 0;j<childsSub;j++){
            if(dmtSubFolder.getChildAt(j).toString().equals(lot)){
                dmtLotFolder = (DefaultMutableTreeNode) dmtSubFolder.getChildAt(j);
                foldLotExists = true;
                j=childsSub;
            }
        }
        if(!foldLotExists){
            dmtLotFolder = new DefaultMutableTreeNode("lote "+lot);
            dmtSubFolder.add(dmtLotFolder);
        }
        dmtLotFolder.add(new DefaultMutableTreeNode(name));

    }
    DefaultTreeModel myTree = new DefaultTreeModel(dmtRoot);

    cbt.setModel(myTree);
}

我认为真正的问题是:

cbt.setModel(myTree);

这是定义Tree内容的正确方法吗?

编辑.: 按钮确定:

...
JButton btnOk = new JButton("OK");
btnOk.setBounds(161, 37, 49, 23);
btnOk.setActionCommand("ok");
btnOk.addActionListener(buttonListener);
panel.add(btnOk);
...

class ButtonListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent a) {
        switch(a.getActionCommand()){
            case "ok":                  
                search();
                self.requestFocusInWindow();
                break;
            case "cancel":
                dispose();
                break;
            case "print":
                TreePath[] tp = cbt.getCheckedPaths();
                for(TreePath t : tp){
                    System.out.println(t.getLastPathComponent().toString());
                }
                self.requestFocusInWindow();
                break;
            default:
                break;
        }
    }
}

private void search(){
    FileSeeker fs = new FileSeeker(textField.getText());
    ArrayList<File> files = fs.getFiles();

    defineNewTree(files);
}

编辑.: CheckBoxCellRenderer:

private class CheckBoxCellRenderer extends JPanel implements TreeCellRenderer {     
    private static final long serialVersionUID = -7341833835878991719L;     
    JCheckBox checkBox;     
    public CheckBoxCellRenderer() {
        super();
        this.setLayout(new BorderLayout());
        checkBox = new JCheckBox();
        add(checkBox, BorderLayout.CENTER);
        setOpaque(false);
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean selected, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        Object obj = node.getUserObject();          
        TreePath tp = new TreePath(node.getPath());
        CheckedNode cn = nodesCheckingState.get(tp);
        if (cn == null) {
            return this;
        }
        checkBox.setSelected(cn.isSelected);
        checkBox.setText(obj.toString());
        checkBox.setOpaque(cn.isSelected && cn.hasChildren && ! cn.allChildrenSelected);
        return this;
    }       
}

1 个答案:

答案 0 :(得分:0)

因此,对于第一个问题,解决方案是:

private void defineEmpityTree(){            
    DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode("Relat\u00F3rios Individuais");
    cbt.setModel(new DefaultTreeModel(dmtn));   
    ((DefaultTreeModel) cbt.getModel()).nodeChanged(dmtn);
}

我只需通知节点已更改。

对于截断问题,

cbt.setLargeModel(true);

做了这个伎俩。