Issue
My friend and I are trying to make a quiz and we both are very new to programming, the problem we have is that we can't get the input from namnSpelare1.setPromptText("Spelare 1"); to be set as the label5 text. This is the line we tried to do it with " label5.setText("Fråga till " + namnSpelare1.getText() + "."); " but it doesn't work.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import com.sun.javafx.scene.LayoutFlags;
import javax.swing.*;
import java.beans.beancontext.BeanContext;
public class Main extends Application {
TextField namnSpelare1 = new TextField();
TextField namnSpelare2 = new TextField();
TextField namnSpelare3 = new TextField();
TextField namnSpelare4 = new TextField();
Label label5 = new Label("");
Stage window;
Scene scene1, scene2, scene3, scene4;
Button buttonRoles, buttonStart, buttonBack1, buttonBack2, buttonDone1, ButtonBack3;
LayoutFlags layout1, layout2, layout3, layout4;
@Override
public void init() throws Exception { //Vad som händer innan programmet startat.
System.out.println("Before");
}
@Override
public void start(Stage primaryStage) throws Exception {
//Vad som händer när programmet startar.
window = primaryStage;
//Sidan "START"
Label label1 = new Label("Vällkommen till Quizera!");
label1.setFont(new Font("Arial", 18));
buttonStart = new Button("START");
buttonRoles = new Button("REGLER");
buttonRoles.setOnAction(e -> window.setScene(scene2));
buttonStart.setOnAction(e -> window.setScene(scene3));
VBox layout1 = new VBox(20);//Radavstånd
layout1.setPadding(new Insets(30, 50, 100, 185)); //Tak, Höger Vägg, Golvet, Vänster Vägg
buttonStart.setPadding(new Insets(30, 88.5, 30, 88.5));
buttonRoles.setPadding(new Insets(30, 85, 30, 85));
layout1.getChildren().addAll(label1, buttonStart, buttonRoles);
scene1 = new Scene(layout1, 600, 300);
//Sidan "Regler"
Label label2 = new Label("Regler");
label2.setFont(new Font("Arial", 18));
Label label4 = new Label("Den spelare som först får 7 rätt vinner.");
label4.setFont(new Font("Arial", 11));
Button buttonBack1 = new Button("TILLBAKA");
buttonBack1.setOnAction(e -> window.setScene(scene1));
VBox layout2 = new VBox(20);
layout2.setPadding(new Insets(30, 50, 100, 185)); //Tak, Höger Vägg, Golvet, Vänster Vägg
buttonBack1.setPadding(new Insets(10, 88.5, 10, 88.5));
layout2.getChildren().addAll(label2, label4, buttonBack1);
scene2 = new Scene(layout2, 600, 300);
window.setScene(scene1);
window.setTitle("Quizera");
window.show();
//Creating a GridPane container
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(5);
grid.setHgap(5);
//Sidan "Spelarnas namn?"
Label label3 = new Label("Fyll i spelarnas namn.");
label3.setFont(new Font("Arial", 18));
namnSpelare1.setPromptText("Spelare 1");
namnSpelare2.setPromptText("Spelare 2");
namnSpelare3.setPromptText("Spelare 3");
namnSpelare4.setPromptText("Spelare 4");
//Button button = new Button("TILLBAKA");
Button buttonDone1 = new Button("KLAR");
Button buttonBack2 = new Button("TILLBAKA");
buttonBack2.setOnAction(e -> window.setScene(scene1));
buttonDone1.setOnAction(e -> window.setScene(scene4));
VBox layout3 = new VBox(12);
buttonBack2.setPadding(new Insets(8, 88.5, 8, 88.5));
buttonDone1.setPadding(new Insets(8, 100, 8, 100));
layout3.setPadding(new Insets(18, 185, 100, 185));//Tak, Höger Vägg, Golvet, Vänster Vägg
layout3.getChildren().addAll(label3, namnSpelare1, namnSpelare2, namnSpelare3, namnSpelare4, buttonDone1, buttonBack2);
scene3 = new Scene(layout3, 600, 300);
//Fråga 1
int randomNum = (int)(Math.random() * 3);
String[] questions = {"Vilken färg har ett blåbär?", "Vad heter Isabel?", "Hur hög är en två meter hög låda"};
String[] answers = {"blå", "isabel", "två meter"};
String namn1 = (namnSpelare1.getText());
Label label6 = new Label(questions[randomNum]);
label5.setFont(new Font("Arial", 11));
Button buttonBack3 = new Button("TILLBAKA");
label5.setText("Fråga till " + namnSpelare1.getText() + ".");
buttonBack3.setOnAction(e -> window.setScene(scene3));
VBox layout5 = new VBox(20);
layout5.setPadding(new Insets(30, 50, 100, 185)); //Tak, Höger Vägg, Golvet, Vänster Vägg
//StackPane layout5 = new StackPane();
layout5.getChildren().addAll(label5, label6, buttonBack3);
scene4 = new Scene(layout5, 600, 300);
window.setTitle("Quizera");
window.show();
}
//Vad som händer efter programmet stängts ner.
public void stop() throws Exception {
System.out.println("After");
}
public static void main (String[] args) {
launch();
}
}
Solution
GUI frameworks such as JavaFX are event driven. The TextField text is dynamic and can change, generating an event which you can take action on.
When the TextField text changes, update the Label text to match the new value.
You can do this using either an event handler or a binding.
Event handling (property change listener) solution
See Value Change Listener for JavaFX's TextField
namnSpelare1.textProperty().addListener((observable, oldValue, newValue) ->
label5.setText("Fråga till " + ((newValue != null) ? newValue : "") + ".")
);
Binding solution
See Javafx concatenation of multiple StringProperty
label5.textProperty().bind(
Bindings.concat(
"Fråga till ", namnSpelare1.textProperty(), "."
)
);
Note the ,
usage in the above statement instead of +
, it's important...
Background documentation on properties and binding.
Answered By - jewelsea