2017-09-26 2 views
2

Lorsque vous créez un prospect SpanButton pour un conteneur, il ne reçoit pas les événements. J'ai essayé le débogage mais je n'ai pas réussi à comprendre pourquoi cela ne fonctionne pas. Cela semble être une erreur CN1, mais j'ai peut-être mal compris quelque chose.Comment faire fonctionner un SpanButton lorsqu'il est fait?

Voici un test de reproduire le problème:

Form hi = new Form("Welcome", BoxLayout.y()); 

    //test case shows that when you make a normal Button lead for a Container, 
    //it works as expected. 
    //Doing the same with a SpanButton doesn't work. 
    //When you click button it correctly hides the Label. 
    //Clicking spanButton doesn't hide the label. 
    // 
    //Test also shows a second issue: when adding a Command to a SpanButton, 
    //the Command text is shown in parallel with the SpanButton's text 


    //edit Button works normal 
    Button button = new Button("Normal Button works"); 
    Label hide2 = new Label("Now you see me, now you don't2"); 
    Command cmd2 = Command.create("CmdTxt2", null, (ev) -> { 
     hide2.setHidden(!hide2.isHidden()); 
     hi.revalidate(); 
    }); 
    button.setCommand(cmd2); 
    Label editButtonLabel2 = new Label(); 
    FontImage.setMaterialIcon(editButtonLabel2, FontImage.MATERIAL_CHEVRON_RIGHT); // [>] 

    Container cont2 = BorderLayout.centerEastWest(button, editButtonLabel2, null); 
    cont2.setLeadComponent(button); //for a normal button, the lead works correctly 
    hi.add(cont2); 
    hi.add(hide2); 

    //with SpanButton it doesn't work: 
    SpanButton spanButton = new SpanButton("SpanButton does not work"); 
    Label hide = new Label("Now you see me, now you don't"); 
    Command cmd = Command.create("CmdText", null, (ev) -> { 
     hide.setHidden(!hide.isHidden()); 
     hi.revalidate(); 
    }); 
    spanButton.setCommand(cmd); 
    Label editButtonLabel = new Label(); 
    FontImage.setMaterialIcon(editButtonLabel, FontImage.MATERIAL_CHEVRON_RIGHT); // [>] 

    Container cont = BorderLayout.centerEastWest(spanButton, editButtonLabel, null); 
    cont.setLeadComponent(spanButton); //spanButton made lead for cont, so should receive all events for cont, but doesn't 
    hi.add(cont); 
    hi.add(hide); 


    hi.show(); 

Répondre

1

Serait-ce dû à SpanButton être un conteneur lui-même et ayant également un composant principal qui est un Button?

Vous pouvez créer un Button normal et lui appliquer cette commande, puis définir le composant principal du conteneur sur Button. Vous ne devez pas ajouter ce Button au conteneur, ajoutez votre SpanButton et le conteneur recevra toujours tous les événements.

Form hi = new Form("Welcome", BoxLayout.y()); 

/*test case shows that when you make a normal Button lead for a Container, 
it works as expected. 
Doing the same with a SpanButton doesn't work. 
When you click the button, it correctly hides the Label. 
Clicking spanButton doesn't hide the label. 

Test also shows a second issue: when adding a Command to a SpanButton, 
the Command text is shown in parallel with the SpanButton's text*/ 


//edit Button works normal 
Button button = new Button("Normal Button works"); 
Label hide2 = new Label("Now you see me, now you don't2"); 
Command cmd2 = Command.create("CmdTxt2", null, (ev) -> { 
    hide2.setHidden(!hide2.isHidden()); 
    hi.revalidate(); 
}); 
button.setCommand(cmd2); 
Label editButtonLabel2 = new Label(); 
FontImage.setMaterialIcon(editButtonLabel2, FontImage.MATERIAL_CHEVRON_RIGHT); // [>] 

Container cont2 = BorderLayout.centerEastWest(button, editButtonLabel2, null); 
cont2.setLeadComponent(button); //for a normal button, the lead works correctly 
hi.add(cont2); 
hi.add(hide2); 

//with SpanButton it doesn't work: 
SpanButton spanButton = new SpanButton("SpanButton does not work"); 
Label hide = new Label("Now you see me, now you don't"); 
Command cmd = Command.create("CmdText", null, (ev) -> { 
    hide.setHidden(!hide.isHidden()); 
    hi.revalidate(); 
}); 
Button btnHidden = new Button(cmd); 

Label editButtonLabel = new Label(); 
FontImage.setMaterialIcon(editButtonLabel, FontImage.MATERIAL_CHEVRON_RIGHT); // [>] 

Container cont = BorderLayout.centerEastWest(spanButton, editButtonLabel, null); 
cont.setLeadComponent(btnHidden); //spanButton made lead for cont, so should receive all events for cont, but doesn't 
hi.add(cont); 
hi.add(hide); 


hi.show(); 
+1

Oui. Vous ne pouvez pas faire d'un composant lead un leader car cela va dans une boucle. –

+1

Merci Shai, pouvez-vous documenter cela (j'ai gaspillé un jour de débogage parce que vous pensez facilement qu'un SpanButton peut être utilisé en remplacement d'un bouton normal)? Et merci Diamond, je vais essayer votre suggestion qui, je pense, va fonctionner. – user1246562