I've been trying to make a Christmas tree decorating, and I try to apply the command design pattern to my buttons by making a commandInterface, buttonHandler class, and button classes as command objects. The class XmasTreeSwing contains the Gui elements and the buttons, below is the code for Button Handler and Command Interface. the Button classes implement the interface and the code goes like this. At run time, the Button handler class throws a cast exception. I want the ButtonHandler class to pass the commands to the proper Command object (lightButton in this example). Inside the XmasTreeClass:
ButtonHandler handler = new ButtonHandler();
lightButton.addActionListener(handler);
ornamentButton.addActionListener(handler);
The commandInterface:
public interface CommandInterface{
public void processEvent();
}
The ButtonHandler class:
public class ButtonHandler extends JButton implements ActionListener {
@Override //coding the event handling routine
public void actionPerformed(ActionEvent event) {
CommandInterface command = event.getSource();
}
And Finally the LightButton class:
public class lightButton extends JButton implements CommandInterface {
public lightButton() {
}
@Override
public void processEvent() {
//Some code
}
public lightButton(String name) {
super(name);
}
}//class