Skip to main content

actionPerformed with loop

11 replies [Last post]
heziflash
Offline
Joined: 2008-06-03

Hi

1. the code hereby is not excuting actionPerformed well. why?
2. how can i debug actionPerformed?
3. what is the right way to write it?
for ( i = 0; i < 30; i++) {
.
.
create Button
.
.
c.addComponent(cb[ i ]);
cb[ i ].addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (cb[0].isSelected())
System.out.println("cb "+i);
}
});
}
TIA
Hezi

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
heziflash
Offline
Joined: 2008-06-03

Hi myinstinc

The problem is, when creating CheckBox, u dont have : public Button(Command cmd) so, i cant use the getId.
TIA
Hezi

heziflash
Offline
Joined: 2008-06-03

Hi Chen

I'm trying to make a loop from 0 to n and in the loop , I want to create n CheckBoxes and n ActionPerformed.
I succeeded to build n's CheckBoxess but, I did not succeed to build n's ActionPerformed.
If u will see at the beginning of the question, u can see how I build the code (but instead of create Button I want to create
Thanks
Hezi

myinstinct
Offline
Joined: 2008-10-24

I've never done a long list of checkboxes, but checkBox just extends Button, so it shouldn't be a lot different

instead of 30 buttons (or checkboxes) with 30 actionlisteners - think of it as 30 buttons (or cbs) and 1 actionlistener that listens to those 30 buttons (or cbs)

heziflash
Offline
Joined: 2008-06-03

Hi

can any one tell me pls, if instead of creting Button, i want to create CheckBox?
TIA
Hezi

Chen Fishbein

Hi Hezi,
What are you trying to accomplish?

Regards,
Chen

lwuit-users@mobileandembedded.org wrote:
> Hi
>
> can any one tell me pls, if instead of creting Button, i want to create CheckBox?
> TIA
> Hezi
> [Message sent by forum member 'heziflash' (heziflash)]
>
> http://forums.java.net/jive/thread.jspa?messageID=320957
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

heziflash
Offline
Joined: 2008-06-03

Hi myinstinc

thank u very much for ur answer.

can u tell me pls, if intesd of creting Button, i want to create CheckBox?
TIA
Hezi

heziflash
Offline
Joined: 2008-06-03

Hi myinstinc

than u for ur reply.
i have another question:

Command cmd = ae.getCommand();

how is "ae" declared?
TIA
Hezi

myinstinct
Offline
Joined: 2008-10-24

my apologies, that is a typo - using your code it should be

evt instead of ae

Command cmd = evt.getCommand();

heziflash
Offline
Joined: 2008-06-03

Hi shaii28

thank u for ur reply.

cb-->0 was just an example.
but for u i changed it to cb-->i.
i PUT A BREAKPOINT AFTER THE THE isSelectd AND BEFORE. BUT, IT NEVER REACHES THE BREAKPOINT!!!

shaii28
Offline
Joined: 2007-02-05

well for once .........
i think the "if" in your actionPerformed has a logic bug
"cb[0].isSelected()"

should be "cb[ i ].isSelected()" no?

and u can debug it BY PUTING A BREAK POINT on the if statement
thats like programmng 101....

Message was edited by: shaii28

myinstinct
Offline
Joined: 2008-10-24

I don't know if this is the correct way, but this is what I do in these cases

ActionListener myActionListener = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Command cmd = ae.getCommand();
System.out.println("myButton " + cmd.getId() + " was pressed");
}
}

for ( i = 0; i < 30; i++) {

Button myButton = new Button(new Command("Button " + Integer.toString(i), i));

myButton.addActionListener(myActionListener);

c.addComponent(myButton);
}