devxlogo

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

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

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.