I'm trying to implement a GUI in JavaFX for a text-based game I've been making.
This part of the main class sets everything up:
public class Main extends Application{
@FXML
protected TextField input;
@FXML
protected TextArea output, inventory, commands;
protected static List<String> history;
protected static int historyPointer;
protected static String textToRead = null;
private Service<Void> backgroundThread;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("Console.fxml"));
BorderPane root = (BorderPane) loader.load();
history = new ArrayList<>();
historyPointer = 0;
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("MyConsoleFXGUI"); //Could later be changed so that the actual game title is displayed here.
stage.show();
I use a FXML-file generated from SceneBuilder and Main is the controller. It works well and when I tried to set some text to input through the initialize function, the text printed fine (but I have now removed that method).
The problem comes when I then launch my Game-class and try to print text from it to the text area "Input" in main.
I use this method in Main to set the text:
/**
* Called when the game wants to print something to the game
* @param message The text to be printed to the console.
*/
public void printGameInfo(String message) {
System.out.println("This method was attempted!");
output.setText(message + System.lineSeparator());
}
This method should work, the problem I have is that I don't know how to call it from the Game-class. Since the Main class isn't instantiated I can't call on a Main-object and I can't make the text area static as that doesn't work with JavaFx applications.
So how do I go about to call the "printGameInfo" from a separate class to set some strings to a text area?
Thanks a lot!