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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: User Interface Design
Expertise: Beginner
Mar 19, 1997

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Changing Button Captions

Question:
How do you change a button's caption after it is created?

Answer:
Java's Button class defines two methods for getting and setting a button's caption. They are getLabel() and setLabel(). The following simple applet creates a button whose caption changes when the user presses the button:


import java.awt.*;
import java.applet.*;
 
public class ButtonLabel extends Applet {
        Button b;
        boolean active = false;

        // define a button with some initial label
        public void init() {
                b = new Button("Inactive");
                add(b);
        }

        // Change the caption when the user presses the button
        // after it has been created 
        public synchronized boolean handleEvent(Event e) {
                if (e.target instanceof Button) {
                        if (active) {
                                active = false;
                                b.setLabel("Inactive");
                        } else {
                                active = true;
                                b.setLabel("Active");
                        }
                        return true;
                }
                return false;
        }
 
        //
        // Defining a main() routine allows the applet to be
        // run as a standalone Java application also
        //
        public static void main(String argv[]) {
                Frame f = new Frame("ButtonLabel");  
                Applet a = new ButtonLabel();
                a.init();
                a.start();
 
                f.setLayout(new BorderLayout());
                f.add("Center", a);
                f.pack();
                f.show();
        }
}
 
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