Is there any way to move actionListener classes to stand-alone classes?
I made an exaple using Java and MVC design pattern. I have 3 buttons that change background color.
Here's Model
public class ChangeFontColorApplicationModel {
public ChangeFontColorApplicationModel(){}
}
View
import java.awt.event.*;
import javax.swing.*;
public class ChangeFontColorApplicationView extends JFrame{
private JButton changeToYellowButton = new JButton("Yellow font");
private JButton changeToBlackButton = new JButton("Black font");
private JButton changeToBlueButton = new JButton("Blue font");
public ChangeFontColorApplicationView(){
super("Change font");
JPanel buttonPanel = new JPanel();
buttonPanel.add(changeToYellowButton);
buttonPanel.add(changeToBlackButton);
buttonPanel.add(changeToBlueButton);
this.add(buttonPanel);
}
public void addButtonActionListener(){
changeToYellowButton.addActionListener(new yellowBackgroundHandler());
changeToBlackButton.addActionListener(new yellowBackgroundHandler());
changeToBlueButton.addActionListener(new yellowBackgroundHandler());
}
}
Controller
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChangeFontColorApplicationController {
private ChangeFontColorApplicationView changeFontColorApplicationViewObject;
backgroundHandler yellowBackgroundHandlerObject = new yellowBackgroundHandler();
backgroundHandler blackBackgroundHandlerObject = new blackBackgroundHandler();
backgroundHandler blueBackgroundHandlerObject = new blueBackgroundHandler();
public ChangeFontColorApplicationController(ChangeFontColorApplicationView changeFontColorApplicationViewObject){
this.changeFontColorApplicationViewObject = changeFontColorApplicationViewObject;
yellowBackgroundHandlerObject.setNextHandlerInChain(blackBackgroundHandlerObject);
blackBackgroundHandlerObject.setNextHandlerInChain(blueBackgroundHandlerObject);
this.changeFontColorApplicationViewObject.addButtonActionListener();
}
}
Now I created an interface that handles the button events
public interface backgroundHandler extends ActionListener{
public void setNextHandlerInChain(backgroundHandler nextInChain);
public void handleCommand(String getActionCommandString);
public void actionPerformed(ActionEvent event);
}
Every class that implements the interfaces, handles the ActionEvent. If it cannot, it passes to the next class (as Chain of responsibility)
yellow font handler
import java.awt.Color;
import java.awt.event.ActionEvent;
public class yellowBackgroundHandler implements backgroundHandler{
private backgroundHandler nextInChain;
private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject = new ChangeFontColorApplicationView();
public yellowBackgroundHandler(){}
@Override
public void setNextHandlerInChain(backgroundHandler nextInChain) {
this.nextInChain = nextInChain;
}
@Override
public void handleCommand(String getActionCommandString) {
if(getActionCommandString.equalsIgnoreCase("Yellow font")){
ChangeFontColorApplicationViewObject.setBackground(Color.yellow);
}
else {
nextInChain.handleCommand(getActionCommandString);
}
}
@Override
public void actionPerformed(ActionEvent event) {
try{
handleCommand(event.getActionCommand());
}
catch (Exception exeption){
ChangeFontColorApplicationViewObject.setTitle(exeption.toString());
}
}
}
black font handler
import java.awt.Color;
import java.awt.event.ActionEvent;
public class blackBackgroundHandler implements backgroundHandler{
private backgroundHandler nextInChain;
private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject = new ChangeFontColorApplicationView();
public blackBackgroundHandler(){}
@Override
public void setNextHandlerInChain(backgroundHandler nextInChain) {
this.nextInChain = nextInChain;
}
@Override
public void handleCommand(String getActionCommandString) {
if(getActionCommandString.equalsIgnoreCase("Black font")){
ChangeFontColorApplicationViewObject.setBackground(Color.BLACK);
}
else {
nextInChain.handleCommand(getActionCommandString);
}
}
@Override
public void actionPerformed(ActionEvent event) {
try{
handleCommand(event.getActionCommand());
}
catch (Exception exeption){
ChangeFontColorApplicationViewObject.setTitle(exeption.toString());
}
}
}
blue font handler
import java.awt.Color;
import java.awt.event.ActionEvent;
public class blueBackgroundHandler implements backgroundHandler{
private backgroundHandler nextInChain;
private ChangeFontColorApplicationView ChangeFontColorApplicationViewObject = new ChangeFontColorApplicationView();
public blueBackgroundHandler(){}
@Override
public void setNextHandlerInChain(backgroundHandler nextInChain) {
this.nextInChain = nextInChain;
}
@Override
public void handleCommand(String getActionCommandString) {
if(getActionCommandString.equalsIgnoreCase("Μπλε φόντο")){
ChangeFontColorApplicationViewObject.setBackground(Color.BLUE);
}
else {
ChangeFontColorApplicationViewObject.setTitle("This is the back end of COR");
}
}
@Override
public void actionPerformed(ActionEvent event) {
try{
handleCommand(event.getActionCommand());
}
catch (Exception exeption){
ChangeFontColorApplicationViewObject.setTitle(exeption.toString());
}
}
}
Finally I create the class with the main method
public class ChangeFontColorApp {
public static void main(String[] args){
ChangeFontColorApplicationView changeFontColorApplicationViewObject = new ChangeFontColorApplicationView();
ChangeFontColorApplicationController changeFontColorApplicationControllerObject = new ChangeFontColorApplicationController(changeFontColorApplicationViewObject);
changeFontColorApplicationViewObject.setVisible(true);
changeFontColorApplicationViewObject.setSize(600, 600);
changeFontColorApplicationViewObject.setDefaultCloseOperation(ChangeFontColorApplicationView.EXIT_ON_CLOSE);
}
}
Is there any way to make this work? I don't want to have an inner class for event handling with a multiple if blick in it. Any suggestions would be really appreciated.