0

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!

Felix Eder
  • 325
  • 3
  • 16
  • You should read about JavaFx controllers: http://code.makery.ch/library/javafx-8-tutorial/part2/ – Michał Piątkowski May 25 '17 at 09:58
  • If the `Main` class isn't instantiated, it should not have instance members. – user207421 May 25 '17 at 10:06
  • Don't use the `Application` class as the controller class. Start by creating a separate class for the controller, and go from there. See https://stackoverflow.com/questions/33303167/javafx-can-application-class-be-the-controller-class and https://stackoverflow.com/questions/32081713/javafx-controller-class-not-working – James_D May 25 '17 at 11:39
  • Thanks a lot, I will try just that! – Felix Eder May 28 '17 at 13:02

1 Answers1

0

The Main class definitely is instantiated or how should anything be able to call its start method which is not static? When you instantiate your Game class you could pass it a reference to your Main class instance. Nevertheless this is an awfull software design.

mipa
  • 10,369
  • 2
  • 16
  • 35
  • How is it an awful software design? I'm just trying to improve, so I'm happy for feedback on how to write better code. – Felix Eder May 25 '17 at 12:50