Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Math processing
Expertise: Beginner
Mar 21, 1997

Push Button Increments

Question:
I have this declaration in init: pushButton1 = new Button("0"). In the action class I try to increase the value from 0 through 9 every time I click the button trying pushButton1++, but it gives me an error.
    if(e.target == pushButton1){
           showStatus("You pressed: " + o.toString());
           pushButton1++;
        }                                                   

Answer:
Since pushButton1 is an instance of Button, it doesn't make sense to increment it using the ++ operator. Only integers can be incremented this way.

Try this instead:

   if(e.target==pushButton1) {
      int num = Integer.getValue(pushButton1.getLabel()).intValue();
      AppletContext context = getAppletContext();
      context.showStatus("You pushed " + num);
      pushButton1.setLabel("" + ++num);
      repaint();
   } 
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date