Reputation: 35
Edit: Solution at bottom
This is for a checkers game. After one button is clicked, it waits for a second button to be clicked to swap with it. However, sometimes you might not want to move that button, but once you've clicked on it, there's no turning back because I'm unable to disable it.
From other posts on here, I've seen people use
button.setVisable(false);
this just makes it invisible after the first click.
button.setCancelButton(false);
this doesn't do anything.
button.setDisable(false);
this also doesn't do anything. Edit: And all of these methods have been tried with true as well as false.
private void buttonClicked(Object source) {
boolean bool = false;
if (!(source instanceof Button)) {
return;
}
Button button = (Button) source;
if (clickCounter == 0) {
first = button;
first.setStyle("-fx-background-color: LightGreen");
} else {
second = button;
bool = legalMove(GridPane.getRowIndex(first), GridPane.getColumnIndex(first), GridPane.getRowIndex(second), GridPane.getColumnIndex(second), source);
//swap();
System.out.println("checking the bool " + bool);
}
if(bool == true){
swap();
}
else{
System.out.println("Button disabled");
//first.setVisible(false);
//first.setCancelButton(true);
//first.setDisable(false);
clickCounter = 0;
}
System.out.println(clickCounter + " " + ((Button) source).getText());
clickCounter = ++clickCounter % 2;
}
private void swap() {
int firstRow = GridPane.getRowIndex(first);
int firstCol = GridPane.getColumnIndex(first);
int secondRow = GridPane.getRowIndex(second);
int secondCol = GridPane.getColumnIndex(second);
grid.getChildren().removeAll(first, second);
grid.add(first, secondCol, secondRow);
second.setText(" ");
grid.add(second, firstCol, firstRow);
first.setStyle(null);
}
private boolean legalMove(int firstRow, int firstCol, int secondRow, int secondCol, Object source) {
System.out.println("returned back");
//can only move up/down, not diagonally
if (firstCol != secondCol) {
clickCounter = 0;
System.out.println("Can't move diagonally");
return false;
}
//test if the move is no more than 1 spot away
if ((secondRow - firstRow) != 1 && (firstRow - secondRow) != 1) {
clickCounter = 0;
System.out.println("Can't move more than one spot");
return false;
}
return killCheck();
}
Once the button is clicked, you should be able to either click it again, or click a wrong move, or even maybe I'll have a button on the side that allows the user to disable the button so they may choose a different button to use.
Edit: Solution
So to actually disable the button you have to use
setDisable(true);
But then you also need
setDisable(false);
This way it re-enables it, or something, I don't actually know why that works.
if(bool == false && clickCounter == 1){
System.out.println("Button disabled");
first.setDisable(true);
first.setDisable(false);
first.setStyle(null); //this sets the button color back to normal
clickCounter = 0;
return; //reset counter and return to top of buttonClicked() method
}
Upvotes: 0
Views: 2381
Reputation: 664
I'm not very good at programming, but maybe I can help with the logic.
First, I was going to suggest simply to change the color of the button when it's clicked, but I see you already do that.
If it's obligatory to disable the button, this is what I would do:
Click on button1
=>
button1.setStyle("-fx-background-color: LightGreen"); // Change color
button1.setDisabled(true); // Disables the button - cannot be clicked
"Oh, I changed my mind! I want to use another button." =>
Click anywhere on the GridPane
=>
Show either System.out.println("Can't move diagonally");
or System.out.println("Can't move more than one spot");
=>
button1.setDisabled(false); // Enables the button - can be clicked
Basically, in your code, add these lines at these spots:
............
Button button = (Button) source;
if (clickCounter == 0) {
first = button;
first.setStyle("-fx-background-color: LightGreen");
first.setDisabled(true); // This line
} else {
............
}
............
............
if (firstCol != secondCol) {
clickCounter = 0;
System.out.println("Can't move diagonally");
first.setDisabled(false); // This line
return false;
}
//test if the move is no more than 1 spot away
if ((secondRow - firstRow) != 1 && (firstRow - secondRow) != 1) {
clickCounter = 0;
System.out.println("Can't move more than one spot");
first.setDisabled(false); // This line
return false;
}
............
You might've tried setDisabled(true/false)
, but if it's not at the right spots, of course it might not work. Then again, I don't know where in the code you've tested it.
If you can't access first
button in the legalMove()
method, simply pass it as a parameter.
And last but not least, if you're lazy like me, you can check out setOnMouseClicked() method.
Hopefully this has helped at least a bit.
Upvotes: 1