devxlogo

Push Button Increments

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();   } 

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist