Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

[JTree + JCheckBox] Avertir les noeuds enfants d'une modification

1 réponse
Avatar
sylsau
Bonjour,

Apr=E8s quelques recherches je suis arriv=E9 =E0 trouver une solution (=E0
partir d'un code que j'ai un peu adapt=E9) pour afficher des CheckBox
sur les noeuds d'un JTree.

Maintenantn, j'aimerais pouvoir relier les checkBox d'un noeud p=E8re
avec les checkbox de ses enfants dans l'arbre.
Ainsi, lors d'un cochage sur un noeud p=E8re, faire en sorte de cocher
les checkbox de ses enfants.

Voici mon code :





public class CheckBoxNodeTreeSample {

public static void main(String args[]) {

JFrame frame =3D new JFrame("CheckBox Tree" );

CheckBoxNode accessibilityOptions[] =3D {
new CheckBoxNode("22", false),
new CheckBoxNode("21", false)
};

CheckBoxNode browsingOptions[] =3D {
new CheckBoxNode("14", false),
new CheckBoxNode("13", false),
new CheckBoxNode("12", false),
new CheckBoxNode("11", false)
};

Vector accessVector =3D new NamedVector("Node1",
accessibilityOptions);
Vector browseVector =3D new NamedVector("Node2", browsingOptions);
Object rootNodes[] =3D { accessVector, browseVector };
Vector rootVector =3D new NamedVector("Root", rootNodes);
JTree tree =3D new JTree(rootVector);

CheckBoxNodeRenderer renderer =3D new CheckBoxNodeRenderer();
tree.setCellRenderer(renderer);

tree.setCellEditor(new CheckBoxNodeEditor(tree));
tree.setEditable(true);

JScrollPane scrollPane =3D new JScrollPane(tree);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);

}

}

class CheckBoxNodeRenderer implements TreeCellRenderer {

private JCheckBox node =3D new JCheckBox();
private Color selectionBorderColor;
private Color selectionForeground;
private Color selectionBackground;
private Color textForeground;
private Color textBackground;

protected JCheckBox getNode() {
return node;
}

public CheckBoxNodeRenderer() {

Font fontValue;
fontValue =3D UIManager.getFont("Tree.font" );

if (fontValue !=3D null) {
node.setFont(fontValue);
}

Boolean booleanValue =3D (Boolean)
UIManager.get("Tree.drawsFocusBorderAroundIcon" );
node.setFocusPainted((booleanValue !=3D null) &&
(booleanValue.booleanValue()));

selectionBorderColor =3D
UIManager.getColor("Tree.selectionBorderColor" );
selectionForeground =3D
UIManager.getColor("Tree.selectionForeground" );
selectionBackground =3D
UIManager.getColor("Tree.selectionBackground" );
textForeground =3D UIManager.getColor("Tree.textForeground" );
textBackground =3D UIManager.getColor("Tree.textBackground" );
}

public Component getTreeCellRendererComponent(JTree tree, Object
value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {

String stringValue =3D tree.convertValueToText(value, selected,
expanded, leaf, row, false);
node.setText(stringValue);
node.setSelected(false);
node.setEnabled(tree.isEnabled());

if (selected) {
node.setForeground(selectionForeground);
node.setBackground(selectionBackground);

} else {
node.setForeground(textForeground);
node.setBackground(textBackground);

}

if ((value !=3D null) && (value instanceof DefaultMutableTreeNode)) {

Object userObject =3D ((DefaultMutableTreeNode)
value).getUserObject();

if (userObject instanceof CheckBoxNode) {

CheckBoxNode cboxNode =3D (CheckBoxNode) userObject;
node.setText(cboxNode.getText());
node.setSelected(cboxNode.isSelected());

}

}

return node;
}

}

class CheckBoxNodeEditor extends AbstractCellEditor implements
TreeCellEditor {

CheckBoxNodeRenderer renderer =3D new CheckBoxNodeRenderer();
ChangeEvent changeEvent =3D null;
JTree tree;

public CheckBoxNodeEditor(JTree tree) {
this.tree =3D tree;
}

public Object getCellEditorValue() {

JCheckBox checkbox =3D renderer.getNode();
CheckBoxNode checkBoxNode =3D new CheckBoxNode(checkbox.getText(),
checkbox.isSelected());
return checkBoxNode;

}

public boolean isCellEditable(EventObject event) {
return true;
}

public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row) {

Component editor =3D renderer.getTreeCellRendererComponent(tree,
value,
true, expanded, leaf, row, true);




// editor always selected / focused
ItemListener itemListener =3D new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
if (stopCellEditing()) {
fireEditingStopped();
}
}
};

if (editor instanceof JCheckBox) {
((JCheckBox) editor).addItemListener(itemListener);
}

return editor;

}

}

class CheckBoxNode {
String text;
boolean selected;

public CheckBoxNode(String text, boolean selected) {
this.text =3D text;
this.selected =3D selected;
}

public boolean isSelected() {
return selected;
}

public void setSelected(boolean newValue) {
selected =3D newValue;
}

public String getText() {
return text;
}

public void setText(String newValue) {
text =3D newValue;
}

public String toString() {
return getClass().getName() + "[" + text + "/" + selected + "]";
}
}

class NamedVector extends Vector {

String name;

public NamedVector(String name) {
this.name =3D name;
}

public NamedVector(String name, Object elements[]) {
this.name =3D name;
for (int i =3D 0, n =3D elements.length; i < n; i++) {
add(elements[i]);
}
}

public String toString() {
return "[" + name + "]";
}
}





Je pense bien que le rajout doit se faire dans la m=E9thode
getTreeCellEditorComponent dans la classe CheckBoxNodeEditor mais je
n'arrive pas =E0 voir comme je peux proc=E9der pour avertir proprement les
noeuds enfants du changement.

Quelqu'un aurait une id=E9e de la fa=E7on de proc=E9der ?

Merci d'avance de votre aide.

1 réponse

Avatar
Isammoc
Bonjour,

Après quelques recherches je suis arrivé à trouver une solution (à
partir d'un code que j'ai un peu adapté) pour afficher des CheckBox
sur les noeuds d'un JTree.

Maintenantn, j'aimerais pouvoir relier les checkBox d'un noeud père
avec les checkbox de ses enfants dans l'arbre.
Ainsi, lors d'un cochage sur un noeud père, faire en sorte de cocher
les checkbox de ses enfants.

Voici mon code :


<snip du code>

Je pense bien que le rajout doit se faire dans la méthode
getTreeCellEditorComponent dans la classe CheckBoxNodeEditor mais je
n'arrive pas à voir comme je peux procéder pour avertir proprement les
noeuds enfants du changement.

Quelqu'un aurait une idée de la façon de procéder ?

Merci d'avance de votre aide.



Bonjour,

Je me souviens d'avoir fait un truc du genre...
http://www.javafr.com/codes/JCHECKBOXTREE-JTREE-AVEC-JCHECKBOX-CHAQUE-NOEUD-SOURCE-COMPLET_28052.aspx

J'avais utilisé les SelectionModel pour ca...