Reputation: 461
I am creating a with a model-view-controller algorithm. If I have instances of a JButton defined in my controller class. For instance, I have: a button for reset, quit, undo, redo, and settings.
I have implemented their actionListeners. In my Controller class is where the actionPerformed() method is. I implement all 5 buttons. Right now the buttons are set as enabled. However, when the redo and undo are disabled and I push the reset button they stay disabled. How can I reset the state of the redo and undo buttons.
This is my GameController class specifically actionPerformed:
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof DotButton) {
try {
undo.push((GameModel) gameModel.clone());
} catch (CloneNotSupportedException g) {
g.printStackTrace();
}
if(gameModel.getNumberOfSteps()==0){
int row = ((DotButton)(e.getSource())).getRow();
int column = ((DotButton)(e.getSource())).getColumn();
gameModel.capture(row,column);
gameModel.step();
}
selectColor(((DotButton)(e.getSource())).getColor());
} else if (e.getSource() instanceof JButton) {
JButton clicked = (JButton)(e.getSource());
if (clicked.getText().equals("Quit")) {
/*try {
serialization.serialize(gameModel, "savedGame.ser");
} catch (IOException f) {
f.printStackTrace();
}*/
System.exit(0);
} else if (clicked.getText().equals("Reset")){
reset();
if (clicked.getText().equals("Redo")) {
clicked.setEnabled(true);
}
if (clicked.getText().equals("Undo")) {
clicked.setEnabled(true);
}
} else if (clicked.getText().equals("Redo")) {
if (redo.isEmpty()) {
clicked.setEnabled(false);
}
try {
redo();
} catch (CloneNotSupportedException g) {
g.printStackTrace();
}
}
else if (clicked.getText().equals("Undo")) {
if (undo.isEmpty()) {
clicked.setEnabled(false);
}
try {
undo();
} catch (CloneNotSupportedException g) {
g.printStackTrace();
}
}
else if (clicked.getText().equals("Settings")) {
gameView.settingsMenu();
}
} else if (e.getSource() instanceof JRadioButton) {
JRadioButton clickedR = (JRadioButton)(e.getSource());
if (clickedR.getText().equals("Torus")) {
setting1 = true;
}
if (clickedR.getText().equals("Diagonal")) {
setting2 = true;
}
}
This is my GameView class creating the buttons:
public GameView(GameModel model, GameController gameController){
super("Flood it -- the ITI 1121 version");
this.gameModel = model;
this.gameController = gameController;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setLayout(new GridLayout(gameModel.getSize(), gameModel.getSize()));
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 0, 20));
board = new DotButton[gameModel.getSize()][gameModel.getSize()];
for (int row = 0; row < gameModel.getSize(); row++) {
for (int column = 0; column < gameModel.getSize(); column++) {
board[row][column] = new DotButton(row, column, gameModel.getColor(row,column),
(gameModel.getSize() < 26 ? DotButton.MEDIUM_SIZE : DotButton.SMALL_SIZE));
board[row][column].addActionListener(gameController);
panel.add(board[row][column]);
}
}
add(panel, BorderLayout.CENTER);
buttons = new JButton[5];
buttons[0] = new JButton("Reset");
buttons[1] = new JButton("Quit");
buttons[2] = new JButton("Undo");
buttons[3] = new JButton("Redo");
buttons[4] = new JButton("Settings");
for (JButton button : buttons) {
button.setFocusPainted(false);
button.addActionListener(gameController);
}
JPanel settingsPanel = new JPanel();
settingsPanel.setBackground(Color.WHITE);
settingsPanel.add(buttons[2]);
settingsPanel.add(buttons[3]);
settingsPanel.add(buttons[4]);
JPanel control = new JPanel();
control.setBackground(Color.WHITE);
scoreLabel = new JLabel();
control.add(scoreLabel);
control.add(buttons[0]);
control.add(buttons[1]);
add(settingsPanel, BorderLayout.NORTH);
add(control, BorderLayout.SOUTH);
pack();
setVisible(true);
}
Should I create JButton instances in my GameController class as well as in the GameView.
Upvotes: 0
Views: 997
Reputation: 347184
It's impossible for this condition to ever be met
} else if (clicked.getText().equals("Reset")){
reset();
if (clicked.getText().equals("Redo")) {
If the first if
statement is true
, then the second can never be true
Upvotes: 2