Issue
I am trying to create a simple program for rock paper scissors but I am having trouble adding anything to the pane. I when I do pane.getChildren().add(goButton); it doesn't compile and says that it cannot be applied. I'm sure it is simple but my code looks the same compared to what I see online.
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.event.*;
import java.awt.*;
public class Main extends Application {
// private String[] answers = {"rock", "paper", "scissors"};
// private TextField userOutputTF, compOutputTF;
// private Button goButton;
// private Label title;
// private VBox pane;
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.setTitle("Rock, Paper, Scissors");
VBox pane = new VBox();
pane.setAlignment(Pos.CENTER);
String[] answers = {"rock", "paper", "scissors"};
Button goButton = new Button("Go");
pane.getChildren().add(goButton);
TextField userOutputTF = new TextField("Enter rock, paper, or scissors");
TextField compOutputTF = new TextField("");
compOutputTF.setEditable(false);
userOutputTF.setEditable(true);
goButton.addActionListener(e -> {
System.out.println("Handled Lambda listener");
System.out.println("Have fun!");
});
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public void Deal() {
System.out.println("Hi");
}
}
Solution
You're using AWT controls. Change the imports to use javafx controls, you'll also need to change the button listener:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class Sample extends Application {
// private String[] answers = {"rock", "paper", "scissors"};
// private TextField userOutputTF, compOutputTF;
// private Button goButton;
// private Label title;
// private VBox pane;
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.setTitle("Rock, Paper, Scissors");
VBox pane = new VBox();
pane.setAlignment(Pos.CENTER);
String[] answers = {"rock", "paper", "scissors"};
Button goButton = new Button("Go");
pane.getChildren().add(goButton);
TextField userOutputTF = new TextField("Enter rock, paper, or scissors");
TextField compOutputTF = new TextField("");
compOutputTF.setEditable(false);
userOutputTF.setEditable(true);
goButton.onMouseClickedProperty().addListener((obs, oldValue, newValue) -> {
System.out.println("Handled Lambda listener");
System.out.println("Have fun!");
});
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public void Deal() {
System.out.println("Hi");
}
}
You also need to add the VBox to some other container inside the FXML or just add it to the root.
Answered By - McJoe