1) Jtree に単純なノード (チェック ボックス コントロールなし) を追加する方法。
2) または、Jtree にチェック ボックス ノード (チェック ボックス コントロール付き) を追加する方法。
3) すべてのノードは、Java Swing を使用して Seam ツリーに追加されます。
4) このタスクについて私を提案してください。
以下はコードサンプルです:
public class DynamicTreeView extends JFrame implement 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();
}
}