Algorithm 8.4.3. Algorithm to Swap var1 and var2.
temp = var1
var1 = var2
var2 = temp
ToggleButton
as a JButton
subclass. As its name suggests, it will toggle its label whenever it is clicked, in addition to carrying out some kind of associated action. It will behave like a light switch, toggling between “on” and “off” as it turns the lights on or off.ToggleButton
class
JButton
has just a single label. The main idea in our design is that a ToggleButton
is a JButton
that has two labels, as in Figure 8.4.1.ToggleButton
classActionListener
interface, which will enable it to toggle its label whenever it is clicked.super(l1)
, setting the JButton
label through the first of the two ToggleButton
labels. This initializes the button’s label. This will be the label that appears on the button when it is first displayed.ToggleButton
class.import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ToggleButton extends JButton
implements ActionListener {
private String label1; // Toggle between two labels
private String label2;
public ToggleButton(String l1, String l2) {// Constructor
super(l1); // Use l1 as the default label
label1 = l1;
label2 = l2;
addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String tempS = label1; // Swap the labels
label1 = label2;
label2 = tempS;
setText(label1);
} // actionPerformed()
} // ToggleButton
ToggleButton
adds itself as its own ActionListener
, so whenever it is clicked, its actionPerformed()
method will be invoked.actionPerformed()
method toggles between label1
and label2
and uses the inherited setText()
method to set the buttons label to label1
.temp = var1
var1 = var2
var2 = temp
temp var1 var2 code ---- ---- ---- ---- - 5 7 0. initial values 5 5 7 1. temp = var1 5 7 7 2. var1 = var2 5 7 5 3. var2 = temp - 7 5 4. swapped values
public class Swap {
public static void main(String args[]) {
String var1 = "on", var2 = "off";
System.out.println("var1=" + var1 + " var2=" + var2);
var1 = var2;
var2 = var1;
System.out.println("var1=" + var1 + " var2=" + var2);
}
}
ToggleButton
toggles its label between two values, what about performing an associated action? To do this, we will need multiple event handlers, one to handle the toggling of the button’s label and the other to handle its associated action (Fig 8.4.5).ToggleButton
has two ActionListener
s, each of which will take its own independent action.ToggleButton
itself and the other with the object that uses the ToggleButton
.LightSwitchTester
(Listing 8.4.6). LightSwitchTester
extends JFrame
and implements the ActionListener
interface. The ToggleButton
(named lightSwitch
) simulate a light switch. Whenever it is pressed, the LightSwitchTester
will report whether the light is on or off and the lightSwitch
will toggle its label.LightSwitch
class.import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LightSwitchTester extends JFrame
implements ActionListener {
private ToggleButton lightSwitch;
public LightSwitchTester() {
lightSwitch = new ToggleButton ("off","on");
getContentPane().add(lightSwitch);
LightSwitchTester.addActionListener(this);
} // constructor()
public void actionPerformed(ActionEvent e) {
setTitle("The light is " + lightSwitch.getText());
} // actionPerformed()
public static void main(String args[]) {
JFrame f = new LightSwitchTester();
f.setSize(200,200);
f.setVisible(true);
}
} // LightSwitchTester
lightSwitch
is clicked, the program displays the message, “The light is on,” or “The light is off,” in the program’s title bar (Figure 8.4.7). This is a somewhat trivial action but it illustrates that a ToggleButton
both toggles its own label and carries out some associated action.LightSwitch
button causes “The light is on” or “The light is off” to appear in the window’s title bar.ToggleButton
design satisfies several key design principles of object-oriented programming. First and foremost, it uses inheritance to extend the functionality of the predefined JButton
class—the extensibility principle. Secondly, it encapsulates a ToggleButton
’s essential behavior within the ToggleButton
class itself—the modularity principle. Finally, it hides the mechanism by which a ToggleButton
manages its labels—the information-hiding principle.ToggleButton
does everything that a JButton
does, plus it can toggle its own label.ActionListener
.