2017-10-12 1 views
3

Ich möchte den Text des Befehls in der Titelleiste programmgesteuert ändern, aber es geschieht nicht. Warum ändert sich der Befehlsname "aaa" im folgenden Code nicht in "bbb"?Befehlstext programmgesteuert ändern

labourChargeSumCommand = new Command("") { 

    @Override 
    public void actionPerformed(ActionEvent evt) { 

    } 
}; 
labourChargeSumCommand.setCommandName("aaa"); 
getToolbar().addCommandToRightBar(labourChargeSumCommand); 

cb1.addActionListener(e -> { 
    if (cb1.isSelected()) { 
     labourChargeSumCommand.setCommandName("bbb"); 
     getToolbar().revalidate(); 
    } 
}); 

Update: Alle meine Code

public class MyApplication { 

    private Form current; 
    private Resources theme; 
    Command labourChargeSumCommand; 

    public void init(Object context) { 
     theme = UIManager.initFirstTheme("/theme"); 

     // Enable Toolbar on all Forms by default 
     Toolbar.setGlobalToolbar(true); 

     // Pro only feature 
     Log.bindCrashProtection(true); 
    } 

    public void start() { 
     if (current != null) { 
      current.show(); 
      return; 
     } 
     Form hi = new Form("Hi World", BoxLayout.y()); 
     hi.add(new Label("Hi World")); 
     hi.show(); 

     labourChargeSumCommand = new Command("") { 

      @Override 
      public void actionPerformed(ActionEvent evt) { 

      } 
     }; 
     labourChargeSumCommand.setCommandName("aaa"); 
     hi.getToolbar().addCommandToRightBar(labourChargeSumCommand); 

     Button bb = new Button("bb"); 
     bb.addActionListener(e -> { 
      if (true) { 
       labourChargeSumCommand.setCommandName("bbb"); 
       System.out.println(labourChargeSumCommand.getCommandName()); 
       hi.getToolbar().revalidate(); 
       hi.getToolbar().repaint(); 
      } 
     }); 
     hi.add(bb); 
    } 

} 

Hier habe ich einen BTN hinzugefügt und mit Code innerhalb seiner Aktion Hörer, das ist alles.

+0

Erstes Debug ** cb1.isSelected() ** gibt ** true ** zurück –

+0

yeahh, es gibt true zurück. Was ich tun möchte, ist, wenn ich das Kontrollkästchen anwähle, sollte der Befehl in der Titelleiste geändert werden. Es funktioniert für setTitle() aber nicht hier –

+0

Versuchen Sie getToolbar() aufzurufen. Repaint(); nach getToolbar(). revalidate(); –

Antwort

1

Änderungsbefehl Text programmatisch

ich diesen Code //hi.show gerade Kommentar(); fügen Sie es am Ende hinzu. Becuase of this revalidate() nicht funktioniert, so dass labourChargeSumCommand.setCommandName ("bbb"); Text nicht aktualisiert.

public class MyApplication { 

    private Form current; 
    private Resources theme; 
    Command labourChargeSumCommand; 

    public void init(Object context) { 
     theme = UIManager.initFirstTheme("/theme"); 

     // Enable Toolbar on all Forms by default 
     Toolbar.setGlobalToolbar(true); 

     // Pro only feature 
     Log.bindCrashProtection(true); 
    } 

    public void start() { 
     if (current != null) { 
      current.show(); 
      return; 
     } 
     Form hi = new Form("Hi World", BoxLayout.y()); 
     hi.add(new Label("Hi World")); 
     //hi.show(); 
     labourChargeSumCommand = new Command("") { 

      @Override 
      public void actionPerformed(ActionEvent evt) { 

      } 
     }; 
     labourChargeSumCommand.setCommandName("aaa"); 
     hi.getToolbar().addCommandToRightBar(labourChargeSumCommand); 

     Button bb = new Button("bb"); 
     bb.addActionListener(e -> { 
      if (true) { 
       labourChargeSumCommand.setCommandName("bbb"); 
       System.out.println(labourChargeSumCommand.getCommandName()); 
       hi.getToolbar().revalidate(); 
       hi.getToolbar().repaint(); 
      } 
     }); 
     hi.add(bb); 
     hi.show(); 
    } 

} 
1

Durch Festlegen des Befehlsnamens nach dem Hinzufügen zur Symbolleiste wird der Text nicht geändert.

Ich mache einen neuen Befehl und suche nach dem Index des hinzugefügten Befehls und ersetze ihn durch den neuen.

Dies ist nicht so effizient, noch ist es der beste Weg, aber es ist ein Hack, der für mich funktioniert.

Lassen Sie uns sagen, dass wir den Befehl an der rechten Leiste und seine letzte Komponente in der ToolBar Behälter hinzugefügt (Sie können es finden Position durch Komponenteninspektor):

private void switchCommand(Toolbar t, Command cmd) { 
    try { 
     int pos = t.getComponentCount() - 1; 
     Button cmdButton = new Button(cmd.getCommandName()); 
     cmdButton.setUIID("TitleCommand"); 
     cmdButton.setCommand(cmd); 
     t.replaceAndWait(t.getComponentAt(pos), cmdButton, null); 
     t.getComponentForm().revalidate(); 
    } catch (Exception ex) { 
    } 
} 

Dann habe ich dies tun:

labourChargeSumCommand = Command.create("aaa", null, evt -> {}); 
getToolbar().addCommandToRightBar(labourChargeSumCommand); 

cb1.addActionListener(e -> { 
    if (cb1.isSelected()) { 

     labourChargeSumCommand = Command.create("bbb", null, evt -> {}); 
     switchCommand(getToolbar(), labourChargeSumCommand); 
    } 
}); 


public class MyApplication { 

    private Form current; 
    private Resources theme; 
    Command labourChargeSumCommand; 

    public void init(Object context) { 
     theme = UIManager.initFirstTheme("/theme"); 

     // Enable Toolbar on all Forms by default 
     Toolbar.setGlobalToolbar(true); 

     // Pro only feature 
     Log.bindCrashProtection(true); 
    } 

    public void start() { 
     if (current != null) { 
      current.show(); 
      return; 
     } 
     Form hi = new Form("Hi World", BoxLayout.y()); 
     hi.add(new Label("Hi World")); 
     hi.show(); 

     labourChargeSumCommand = Command.create("aaa", null, evt -> {}); 
     hi.getToolbar().addCommandToRightBar(labourChargeSumCommand); 

     Button bb = new Button("bb"); 
     bb.addActionListener(e -> { 
      if (true) { 
       labourChargeSumCommand = Command.create("bbb", null, evt -> {}); 
       switchCommand(getToolbar(), labourChargeSumCommand); 
       System.out.println(labourChargeSumCommand.getCommandName()); 
      } 
     }); 
     hi.add(bb); 
    } 

}