1
我需要在單個樹中呈現兩種類型的節點。單個複選框節點樹中的兩種節點類型
父節點和葉節點。
我也需要編輯它們兩個。
的CheckBoxNodeRender如下:
public JCheckBox leafRenderer;
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
Component returnValue;
if (leaf) {
String stringValue =
tree.convertValueToText(value, selected, expanded, leaf,
row, false);
if (selected) {
leafRenderer.setForeground(selectionForeground);
leafRenderer.setBackground(selectionBackground);
} else {
leafRenderer.setForeground(textForeground);
leafRenderer.setBackground(textBackground);
}
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject =
((DefaultMutableTreeNode) value).getUserObject();
if (userObject instanceof CheckBoxNode) {
CheckBoxNode node = (CheckBoxNode) userObject;
leafRenderer.setText(node.getText());
System.err.println("Leaf Value = "+node.getText());
leafRenderer.setSelected(node.isSelected());
}
}
returnValue = leafRenderer;
}
else {
// For the Parent Node
leafRenderer.setText(value.toString());
leafRenderer.setSelected(selected);
returnValue = leafRenderer;
}
return returnValue;
}
而且編輯如下:
public boolean isCellEditable(EventObject event) {
//All cells are editable
return true;
}
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row) {
Component editor =
renderer.getTreeCellRendererComponent(tree, value,
true, expanded, leaf, row, true);
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
if (stopCellEditing()) {
fireEditingStopped();
}
}
};
if (editor instanceof JCheckBox) {
((JCheckBox) editor).addItemListener(itemListener);
}
return editor;
}
我一直在這裏面臨着一個獨特的問題。 當我選擇父節點並選擇其他節點時,爲父節點設置另一個節點的值。 任何線索? 我在這裏做什麼錯誤?
你能給我一個片段,我怎麼能做到這一點? – Achilles 2012-02-02 04:11:45