1

1)如何在Jtree中添加简单节点(无复选框控件)。

2)或者如何在Jtree中添加复选框节点(带有复选框控件)。

3)使用java swing将所有节点添加到接缝树中。

4)请建议我完成这项任务。

以下是代码示例:

公共类 DynamicTreeView 扩展 JFrame 实现 ActionListener、MouseListener {

// cerate varible  
//private CheckTreeSelectionModel selectionModel;
private JTree tree;
private JTextField textBox;
private JButton btnCreateNode;
private JCheckBox chkNode, ch1;
private JScrollPane ScrollPan;
private JPanel pane, pane1;
private DefaultTreeModel model;
private JCheckBox check;
int hotspot;

public DynamicTreeView() {
    hotspot = new JCheckBox().getPreferredSize().width;
    //cerate Panel object  
    pane = new JPanel();
    //cerate JTextField object  
    textBox = new JTextField(10);
    //cerate JButton object  
    btnCreateNode = new JButton("Create Node");
    //cerate JCheckBox object  
    chkNode = new JCheckBox("Has CheckBox");
    //add componet all to  panel
    pane.add(chkNode);
    pane.add(textBox);
    pane.add(btnCreateNode);
    //Register button object
    btnCreateNode.addActionListener(this);
    this.addMouseListener(this);
    //Cerate the Default Root Tree node for Tree
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Java Tree Example");
    //Cerate the tree object with Default Root Tree node 

    tree = new JTree(root);
    //get the module from tree and add to the DefaultTreeModule
    model = (DefaultTreeModel) tree.getModel();
    //cerate scrollpane object with tree
    ScrollPan = new JScrollPane(tree);
    //scrollpane object add to the form
    //use the border lay out for this form scroll pane is add to the left side of the form
    getContentPane().add(ScrollPan, BorderLayout.WEST);
    //panel object add center of the form
    getContentPane().add(pane, BorderLayout.CENTER);
}

//Main method of the class it's the staring point of the class.
public static void main(String args[]) {
    //set default look of the form on start position.
    DynamicTreeView frame = new DynamicTreeView();
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    if (btnCreateNode.equals(e.getSource())) {

        if (chkNode.isSelected() == true) {
            ch1 = new JCheckBox(textBox.getText());
            TreeCellRenderer render = new TreeCellRenderer() {
                @Override
                public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                    Component returnValue;
                    if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
                        Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
                        ch1.setText(textBox.getText());
                        ch1.setSelected(selected);
                    }
                    returnValue = ch1;
                    return returnValue;
                }
            };
            JOptionPane.showMessageDialog(this, ch1);
            JTree t = new JTree();
            t.setCellRenderer(render);
            MutableTreeNode newNode = new DefaultMutableTreeNode(t);
            //model = (DefaultTreeModel) tree.getModel();
            TreePath path = tree.getSelectionPath();
            //MutableTreeNode newNode = new DefaultMutableTreeNode();
            MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();
            //model.insertNodeInto(cr, node, node.getChildCount());
        } else {
            CheckRenderer cr = new CheckRenderer();
            cr.setNode(new DefaultMutableTreeNode(textBox.getText()));
            model = (DefaultTreeModel) tree.getModel();
            TreePath path = tree.getSelectionPath();
            MutableTreeNode newNode = cr.getNode();
            MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();
            model.insertNodeInto(newNode, node, node.getChildCount());
        }
    }

    SwingWorker sw = new SwingWorker() {
        @Override
        protected Object doInBackground() throws Exception {
            ((DefaultTreeModel) tree.getModel()).reload();
            tree.revalidate();
            tree.repaint();
            ScrollPan.setViewportView(tree);
            return null;
        }
    };
    sw.execute();
}

}

4

0 回答 0