I want to change the text of the command in title bar programmatically but it is not happening. Why doesn't the command name "aaa" changes to "bbb" in the following code?
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: all my 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);
}
}
Here I have added a btn and kept code inside its action listener, that's all.
change command text programatically
I just comment this code //hi.show(); add it at the end. Becuase of this revalidate() not worked, So that labourChargeSumCommand.setCommandName("bbb"); text not updated.
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();
}
}
Setting the command name after adding it to the Toolbar doesn't change the text.
What I do is creating a new command and look for the index of the added command, and replace it with the new one.
This is not so efficient nor is it the best way, but it's a hack that works for me.
Let's say we added the command to the right bar and its the last component in the ToolBar container (You can find it's position through Component Inspector):
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) {
}
}
Then I do this:
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With