Я работаю над Java и пытаюсь создать JTree с выбираемыми конечными узлами (узлами JCheckBox).
Я нашел этот пример, в котором простое JTree сделан. Затем я нашел здесь классы для средства визуализации и редактора ячеек.
После объединения всего этого я пришел к следующему коду:
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.*;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;
public class TestingJTree {
public static void main(String[] args) {
DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode("GrandFather");
DefaultMutableTreeNode padre = new DefaultMutableTreeNode("father");
DefaultMutableTreeNode tio = new DefaultMutableTreeNode("uncle");
DefaultTreeModel treeModel = new DefaultTreeModel(treeNode);
treeModel.insertNodeInto(padre, treeNode, 0);
treeModel.insertNodeInto(tio, treeNode, 1);
DefaultMutableTreeNode hijo = new DefaultMutableTreeNode("son");
treeModel.insertNodeInto(hijo, padre, 0);
JTree tree = new JTree(treeModel);
tree.setCellRenderer(new CheckBoxNodeRenderer());
tree.setCellEditor(new CheckBoxNodeEditor(tree));
JFrame v = new JFrame();
JScrollPane scroll = new JScrollPane(tree);
v.getContentPane().add(scroll);
v.pack();
v.setVisible(true);
v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class CheckBoxNodeRenderer implements TreeCellRenderer {
private JCheckBox leafRenderer = new JCheckBox();
private DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();
public JCheckBox getLeafRenderer() {
return leafRenderer;
}
public CheckBoxNodeRenderer() {
super();
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
boolean expanded, boolean leaf, int row, boolean hasFocus) {
// TODO Auto-generated method stub
Component returnValue;
if (leaf) {
String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row,
false);
leafRenderer.setText(stringValue);
leafRenderer.setSelected(false);
// leafRenderer.setEnabled(tree.isEnabled());
leafRenderer.setEnabled(true);
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
if (userObject instanceof CheckBoxNode) {
CheckBoxNode node = (CheckBoxNode) userObject;
leafRenderer.setText(node.getText());
leafRenderer.setSelected(node.isSelected());
}
}
returnValue = leafRenderer;
} else {
returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree, value, selected,
expanded, leaf, row, hasFocus);
}
return returnValue;
}
}
class CheckBoxNode {
private String text;
private boolean selected;
public CheckBoxNode(String text, boolean selected) {
this.setText(text);
this.selected = selected;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean newValue) {
selected = newValue;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
class CheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTree tree;
private CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
private ChangeEvent changeEvent = null;
public CheckBoxNodeEditor(JTree tree) {
this.tree = tree;
}
@Override
public Object getCellEditorValue() {
// TODO Auto-generated method stub
JCheckBox checkBox = renderer.getLeafRenderer();
CheckBoxNode checkBoxNode = new CheckBoxNode(checkBox.getText(), checkBox.isSelected());
return checkBoxNode;
}
public boolean isCellEditable(EventObject event) {
boolean returnValue = false;
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
if (path != null) {
Object node = path.getLastPathComponent();
if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
}
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
Object userObject = treeNode.getUserObject();
returnValue = ((treeNode.isLeaf()) && (userObject instanceof CheckBoxNode));
}
}
return returnValue;
}
@Override
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected,
boolean expanded, boolean leaf, int row) {
// TODO Auto-generated method stub
Component editor = renderer.getTreeCellRendererComponent(tree, value, true, expanded, leaf,
row, true);
ItemListener itemListener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if (stopCellEditing()) {
fireEditingStopped();
}
}
};
if (editor instanceof JCheckBox) {
((JCheckBox) editor).addItemListener(itemListener);
}
return editor;
}
}
Проблема в том, что листовые узлы не могут быть выбраны:
Любая помощь будет оценена ... Спасибо!