2016-01-27 2 views
2

Je voudrais 'infini' tourner de mes commandes (la commande de rafraîchissement) dans Codename OneToolbar (jusqu'à ce que le chargement soit terminé, puis je veux arrêter la rotation, mais l'image doit encore être visible). Puis-je changer l'icône pour qu'elle commence la rotation? Comment puis-je atteindre ce résultat? L'icône en rotation ne doit pas pouvoir être cliquable lors de la rotation, mais cela ne devrait pas être si difficile à mettre en œuvre.Commande de rotation infinie (ou bouton) dans la barre d'outils dans Codename One

Répondre

2

Vous pouvez utiliser l'API Toolbar et un peu de hack. J'ai créé les méthodes ci-dessous pour le faire:

private void showTitleProgress(Toolbar t) { 
    int pos = t.getComponentCount() - 1; //If you have a command on the left like back command 
    //int pos = t.getComponentCount(); //If you don't have a command on the left like back command 
    InfiniteProgress ip = new InfiniteProgress(); 
    ip.setAnimation(YourProgressImage); 
    ip.setUIID("icon"); 
    Container contIp = FlowLayout.encloseCenterMiddle(ip); 

    Component.setSameWidth(t.getComponentAt(pos), contIp); 
    Component.setSameHeight(t.getComponentAt(pos), contIp); 
    t.replaceAndWait(t.getComponentAt(pos), contIp, null); 
    t.revalidate(); 
} 

private void hideTitleProgress(Toolbar t, Command cmd) { 
    int pos = t.getComponentCount() - 1; //If you have a command on the left like back command 
    //int pos = t.getComponentCount(); //If you don't have a command on the left like back command 
    Button cmdButton = new Button(cmd.getIcon()); 
    cmdButton.setUIID("TitleCommand"); 
    cmdButton.setCommand(cmd); 
    t.replaceAndWait(t.getComponentAt(pos), cmdButton, null); 
    t.getComponentForm().revalidate(); 
} 

Utilisez-le avec API la barre d'outils et votre formulaire comme ceci:

private Command myCommand = new Command(""); 
Form f = new Form("Test form"); 
Toolbar t = new Toolbar(); 
f.setToolbar(t); 

Command back = new Command("Back") { 

    @Override 
    public void actionPerformed(ActionEvent evt) { 
     //Do stuff 
    } 
}; 
back.putClientProperty("uiid", "BackCommand"); 
f.setBackCommand(back); 
t.addCommandToLeftBar(back); 

myCommand = new Command(YourProgressImage) { 

    @Override 
    public void actionPerformed(ActionEvent evt) { 
     //To show the progress when some actions are being performed 
     showTitleProgress(t); 
     //When you're done, discard the progress and restore your command 
     hideTitleProgress(t, myCommand); 
    } 
} 
myCommand.putClientProperty("TitleCommand", true); 
t.addCommandToRightBar(myCommand); 
f.show(); 
+0

Merci, cela fonctionne! Cependant, les commandes sont dans un conteneur à côté du titre (j'ai 2 commandes), donc je récupère d'abord le conteneur puis les commandes. Mais merci pour votre aide! – Maaike

+0

Je me demande pourquoi l'image de progression infinie est plus petite que l'image originale. J'utilise fontimage pour l'image. Peut-être rembourrer quelque part? Je vais découvrir. A part ça, ça marche bien maintenant. – Maaike