Listing 4:
The Clock Customizer
package mybeans;
import java.awt.*;
import java.beans.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ClockCustomizer extends JPanel
implements Customizer{
protected Clock theClockBean = null;
private JRadioButton button24 =
new JRadioButton("24
Hour Mode");
private JRadioButton button12 =
new JRadioButton("12
Hour Mode");
public ClockCustomizer() {
JLabel topLabel = new JLabel();
topLabel.setFont(
new java.awt.Font("Dialog",
1, 20));
topLabel.setHorizontalAlignment(
SwingConstants.CENTER);
topLabel.setText(
"This controls
the mode of the clock");
this.setLayout(new BorderLayout());
button24.addChangeListener(
new ChangeListener()
{
public
void stateChanged(ChangeEvent e) {
button24Changed(e);}});
this.add(topLabel, BorderLayout.NORTH);
JPanel radioPanel = new JPanel();
this.add(radioPanel, BorderLayout.CENTER);
radioPanel.add(button24, null);
radioPanel.add(button12, null);
ButtonGroup grp = new ButtonGroup();
grp.add(button12);
grp.add(button24);
button12.setSelected(true);
}
public void setObject(Object bean) {
theClockBean = (Clock)bean;
button24.setSelected(
theClockBean.isTwentyFourHourTime());
}
void button24Changed(ChangeEvent e) {
theClockBean.setTwentyFourHourTime(
button24.isSelected());
}
}