[CheckBoxTree] probleme de listener ?
bonjour. en cherchant sur le net j ai trouvé un code source qui me permet de creer un JCheckBoxTree. Jusque la tout va bien, l'ennui c'est que je tentes de l'adapter 'a ma sauce' pour qu'il colle a mes besoins.
dans ce JTREE les noeuds et les feuilles ont des checkboxes, qui peuvent etre cochées ou décochées par l'utilisateur. L'ennui que je rencontre c est que j aimerais effectuer un test lorsqu'une checkbox est cliquées et décider si elle doit etre cochée ou pas.
Le test j'ai réussi a le faire dans cette méthode de mon TreeSelectionModel :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| public void valueChanged(TreeSelectionEvent e)
{
int test = 0;
TreePath[] pathis = e.getPaths();
Object o = pathis[0].getLastPathComponent();
if(!((DefaultMutableTreeNode)o).isLeaf()){
int child = ((DefaultMutableTreeNode)o).getChildCount();
for(int i = 0; i<child;i++){
test = _frame.chkBoxChecked(((DefaultMutableTreeNode)o).getChildAt(i));
if(test != 1){
System.out.println("child already selected");
return;
}
}
}
if(((DefaultMutableTreeNode)o).isLeaf()){
System.out.println("o is leaf");
test = _frame.chkBoxChecked(o);
if(test != 1){
System.out.println("o already selected");
return;
}
}
System.out.println("valueChanged");
System.out.println("o:"+o.toString());
System.out.println("test:"+test);
if (e != null && e.getSource() == this.dtsm && test ==1)
{
TreeSelectionEvent event = (TreeSelectionEvent) e.cloneWithSource(this);
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2)
{
if (listeners[i] == TreeSelectionListener.class)
{
((TreeSelectionListener) listeners[i + 1]).valueChanged(event);
}
}
// if (this.mode == CheckBoxTreeSelectionModel.BUNCH_MODE)
// {
// TreePath os[] = event.getPaths();
// Vector v = new Vector();
// for (int i = 0; i < os.length; i++)
// {
// if (os[i].getParentPath() != null)
// {
// v.add(os[i].getParentPath());
// }
// }
// if (v.size() > 0)
// {
// boolean[] bb = new boolean[v.size()];
// Object[] paths = v.toArray();
// TreePath[] tps = new TreePath[v.size()];
// for (int i = 0; i < v.size(); i++)
// {
// bb[i] = true;
// tps[i] = (TreePath)paths[i];
// System.out.println("test 1:"+paths[i].toString());
//
// }
// System.out.println(v.toArray());
// this.valueChanged(new TreeSelectionEvent(this.dtsm, tps, bb,
// e.getOldLeadSelectionPath(), e.getNewLeadSelectionPath()));
// }
// }
//
}
} |
j ai mis en commentaire le reste du code, et pourtant mes checkboxes continuent detre cochées ou décochées malgré que j'aies tout mis en commentaire, avez vous une idée ?