Identifying Components by Label — Just Say No

It is standard AWT practice to check against the labels of certain components (in particular, buttons) to determine the component with which an event is associated. For instance, the following applet displays three buttons and uses the labels of the buttons to determine which one triggered an action event:

 import java.applet.Applet; import java.awt.*; public class ButtonActionApplet extends Applet { public void init() { Button buttonOne = new Button("Button One"); Button buttonTwo = new Button("Button Two"); Button buttonThree = new Button("Button Three"); add(buttonOne); add(buttonTwo); add(buttonThree); } public boolean action(Event event, Object what) { if(what.equals("Button One")) System.out.println("Button ...

Get Graphic Java™ 1.2, Volume I: AWT, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.