Reputation: 49
I am super new to JavaFX and for my checkers game I want to display who's turn it is above my board. I have read through multiple very similar questions, but it doesn't seem to get me where I want to go. I hope you can help me!
I have a FXML file with the following code:
<HBox fx:id="topBox" alignment="TOP_CENTER" prefHeight="45.0" prefWidth="200.0" spacing="10.0" BorderPane.alignment="CENTER" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<children>
<Label fx:id="labelTurn" text="test" />
<Button fx:id="buttonResign" alignment="CENTER" mnemonicParsing="false" onAction="#doResign" prefHeight="25.0" prefWidth="80.0" text="Resign" />
</children>
</HBox>
In my main:
public class Main extends Application {
Checkers content = new Checkers();
BorderPane border = new BorderPane();
@Override
public void start(Stage primaryStage) throws Exception {
try {
border.setTop(FXMLLoader.load(getClass().getResource("Sample.fxml")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
border.setCenter(createContent());
Scene scene = new Scene(border);
primaryStage.setTitle("Checkers from main");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
My controller:
public class SampleController {
@FXML
private Label labelTurn;
@FXML
private HBox topBox;
@FXML
private Button buttonResign;
private Checkers content = new Checkers();
@FXML
void doResign(ActionEvent event) {
content.resign();
// System.exit(0); //TODO eine tatsächliche Methode eingeben!!
}
public void setLabelTurn(Label labelTurn) {
this.labelTurn = labelTurn;
String setLabelText() {
String s = " ";
if (content.getWhiteTurnPropValue()) {
labelTurn.setText("White, it is your turn.");
s = "White, it is your turn.";
return s;
} else {
labelTurn.setText("Black, it is your turn.");
s = "Black, it is your turn.";
return s;
}
}
@FXML
void initialize() {
assert buttonResign != null : "fx:id=\"buttonResign\" was not injected: check your FXML file 'Sample.fxml'.";
assert topBox != null : "fx:id=\"topBox\" was not injected: check your FXML file 'Sample.fxml'.";
assert labelTurn != null : "fx:id=\"labelTurn\" was not injected: check your FXML file 'Sample.fxml'.";
labelTurn.textProperty().setValue(setLabelText());
}
}
My game data has a BooleanProperty to show who's turn it is. It gets changed after every successful move.
BooleanProperty whiteTurnProp = new SimpleBooleanProperty(true);
public BooleanProperty getWhiteTurnProp() {
return whiteTurnProp;
}
public void setWhiteTurnPropValue(Boolean whiteTurn) {
this.whiteTurnProp.setValue(whiteTurn);
}
public boolean getWhiteTurnPropValue() {
return this.whiteTurnProp.getValue();
}
As far as I have understood, I need to bind the Label to the value, which I have done in the initialize method. But when I play and the value in my game class changed, the label stays the same. How do I update the label? I think I need to add a change listener to something, but I dont know how to do that and more specifically WHERE.
Thank you so much for your help! I shortened my code a bit and hope it is still not too long.
Best, Lisa
Upvotes: 4
Views: 1848
Reputation: 21799
In initialize
: labelTurn.textProperty().setValue(setLabelText());
is not a binding, it is a simple set on the property. A really strange one as setLabelText()
sets the text of the Label
and also return the string which was set, which value is used again to the text of the Label
.
You could create a real binding instead:
labelTurn.textProperty().bind(Bindings.createStringBinding(() -> {
String s = " ";
if (content.getWhiteTurnProp().get())
s = "White, it is your turn.";
else
s = "Black, it is your turn.";
return s;
}, content.getWhiteTurnProp()));
Upvotes: 5