What I am dealing with :
I am working on a window with several Checkboxes Trees (Java Swing) and I used samples of codes that I found in stackoverflow to make a JTree with JCheckBox objects. So I made a class CheckBoxNode, a class CheckBoxNodeRenderer and a class CheckBoxNodeEditor. Nevertheless, I don't understand what really happens when I check a box in a tree. I used a very simple "println" to see what happens in many functions like getTreeCellRendererComponent of the class CheckBoxNodeRenderer for instance.
What I would like to do :
I have three trees and I would like to get all the checked elements of each tree in a separate element, an array or an ArrayList. So then, I can work with the selected elements with just reading these arrays or ArrayLists. Of course, if I uncheck a box, the element must be removed from the array.
What I already tried :
I have to admit that I didn't have many ideas. I tried to understand exactly what happens when I check a box, with logs in many functions and I could observe that when I check an element in a tree, my logs are written more than three times (for instance a log in getTreeCellRenderer).
My code :
class CheckBoxNode extends DefaultMutableTreeNode {
/**
*
*/
private static final long serialVersionUID = 1155080395876568819L;
private String _title;
private boolean _selectedStatus;
public CheckBoxNode(String name, boolean isSelected) {
this._title = name;
this._selectedStatus = isSelected;
}
//GETTERS
public String getTitle() {
return this._title;
}
public boolean isSelected() {
return this._selectedStatus;
}
//SETTERS
public void setTitle(String newTitle) {
this._title = newTitle;
}
public void setSelected(boolean isSelected) {
this._selectedStatus = isSelected;
}
public String toString() {
return _title;
}
}
CheckBoxNodeEditor
class CheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {
/**
*
*/
private static final long serialVersionUID = 2616799535763664121L;
private CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
//CONSTRUCTOR
public CheckBoxNodeEditor() {}
//METHODS
public Object getCellEditorValue() {
JCheckBox checkBox = renderer.getNodeRenderer();
CheckBoxNode checkBoxNode = new CheckBoxNode(checkBox.getText(), checkBox.isSelected());
return checkBoxNode;
}
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean isSelected, boolean expanded, boolean leaf, int row) {
Component editor = renderer.getTreeCellRendererComponent(tree, value,
true, expanded, leaf, row, true);
// editor always selected / focused
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
if (stopCellEditing()) {
fireEditingStopped();
}
}
};
if (editor instanceof JCheckBox) {
((JCheckBox) editor).addItemListener(itemListener);
}
return editor;
}
}
CheckBoxNodeRenderer
class CheckBoxNodeRenderer implements TreeCellRenderer {
private JCheckBox nodeRenderer = new JCheckBox();
private Color selectionForeground, selectionBackground, textForeground, textBackground;
//CONSTRUCTOR
public CheckBoxNodeRenderer() {
Font fontValue;
fontValue = UIManager.getFont("Tree.font");
if (fontValue != null) {
nodeRenderer.setFont(fontValue);
}
Boolean booleanValue = (Boolean) UIManager.get("Tree.drawsFocusBorderAroundIcon");
nodeRenderer.setFocusPainted((booleanValue != null)&& (booleanValue.booleanValue()));
selectionForeground = UIManager.getColor("Tree.selectionForeground");
selectionBackground = UIManager.getColor("Tree.selectionBackground");
textForeground = UIManager.getColor("Tree.textForeground");
textBackground = UIManager.getColor("Tree.textBackground");
}
//METHODS
protected JCheckBox getNodeRenderer() {
return nodeRenderer;
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
Component returnValue;
String stringValue = tree.convertValueToText(value, selected, expanded,
leaf, row, false);
nodeRenderer.setText(stringValue);
nodeRenderer.setSelected(false);
nodeRenderer.setEnabled(tree.isEnabled());
if (selected) {
nodeRenderer.setForeground(selectionForeground);
nodeRenderer.setBackground(selectionBackground);
} else {
nodeRenderer.setForeground(textForeground);
nodeRenderer.setBackground(textBackground);
}
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode) value)
.getUserObject();
if (userObject instanceof CheckBoxNode) {
CheckBoxNode node = (CheckBoxNode) userObject;
nodeRenderer.setText(node.getTitle());
nodeRenderer.setSelected(node.isSelected());
}
}
returnValue = nodeRenderer;
return returnValue;
}
}
Let me know if you need more information about my code and I'm very grateful to all of you.
Aucun commentaire:
Enregistrer un commentaire