Skip to main content

Dynamically change Command label

4 replies [Last post]
peter_budo
Offline
Joined: 2006-10-18
Points: 0

I have Form in which I placed CustomItem so I can display things in more convenient way. There is no issue between Form-CustomItem communication. I'm able to recognise which item is selected and depending on it forward to next screen. However I would like that if item has a value Command label should read "Edit" where if there is no value it should read "Add". So far I did not come up with any solution how to change Command label from first item with value and "Edit" to second value and "Add" label on Command.

Any suggestions are welcome.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jaywayjohan
Offline
Joined: 2008-06-18
Points: 0

Hi,

have you tried to use the Item.notifyStateChanged() to communicate any changes in your CustomItem. Or do you want to communicate the other way?

It should be possible to remove a Command and the add a new Command object. This way you could change the Command associated with your CustomItem.

Best Regards
Johan

peter_budo
Offline
Joined: 2006-10-18
Points: 0

Notifications and Threads is something which I still did not grasp, they are elusive topics for me as so far I did not found article that could explain to me in the way I could understand. Hopefully Java - Concurrency in Practice should help, but I just started to read it. After modification code still does not work correctly, I guess it is my lack of knowledge. If you be so kind and have look on the code and correct/suggest a correction to the code

Here is the main body of the form with state listener

[code] public void displayForm() {
customNewContact =
new CustomNewContact(Display.getDisplay(organiser.ElectronicPersonalOrganiser.instance), contact);
selectedMenuCommand = getItemCommand(customNewContact.getSelectedItem());

customNewContact.setDefaultCommand(selectedMenuCommand);
customNewContact.setItemCommandListener(new ItemCommandListener(){
public void commandAction(Command c, Item i){
//decide where to go
goTo(customNewContact.getSelectedItem());
}
});
itemStateChanged(customNewContact);
append(customNewContact);

backCommand = new Command("Back", Command.BACK, 2);
addCommand(backCommand);
setCommandListener(this);
}

public void itemStateChanged(Item item) {
if (item == customNewContact) {
System.out.println("Notification received");
customNewContact.removeCommand(selectedMenuCommand);

customNewContact.setDefaultCommand(selectedMenuCommand);
customNewContact.setItemCommandListener(new ItemCommandListener() {
public void commandAction(Command c, Item i) {
//decide where to go
goTo(customNewContact.getSelectedItem());
}
});
}
}[/code]

plus traverse method from CustomItem that calls notifyStateChanged()

[code] protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {

if (paintDone) {
if (dir == Canvas.UP) {
setSelectedItem(-1);
repaint();
this.notifyStateChanged();
System.out.println("Notified");
}
if (dir == Canvas.DOWN) {
setSelectedItem(1);
repaint();
this.notifyStateChanged();
System.out.println("Notified");
}
}
return true;
}[/code]

peter_budo
Offline
Joined: 2006-10-18
Points: 0

I was close to working solution.
I forgotten to include
[code]this.setItemStateListener(this);[/code]
in my Form. Silly me...

jaywayjohan, thank you for pointing me in right direction.

jaywayjohan
Offline
Joined: 2008-06-18
Points: 0

Glad that I could help!

Regards
Johan