1

Part of my current project is to read a .txt log file, if that log file doesn't exist in it's normal path, I want to open up a JavaFX FileChooser, and let the user point to the file. But in it's current state it pops up the file chooser window, and after I've pointed to the location of the .txt file, it just idles the entire program.

In the class that extends Application:

public class GetDirectory extends Application {

    @Override
    public void start(final Stage primaryStage) {
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter extFilter = new   FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
        fileChooser.getExtensionFilters().add(extFilter);
        File file = fileChooser.showOpenDialog(primaryStage);
        main.gamedata.LogReader logReader = new LogReader();
        logReader.setPathTemp(file.getAbsolutePath());
        primaryStage.close();
    }
}

In the method in another class where I need the path to the file:

    private List<String> getHearthstoneLogContent(String pathToFile) {
        try {
            File file = new File(pathToFile);
            if (file.exists() && !file.isDirectory()) {
                return Files.readAllLines(Paths.get(pathToFile));
            } else {
                // The out_log.txt file does not exist, we should let the user choose the path.
                Application.launch(GetDirectory.class);
                System.out.println("sup");
                return Files.readAllLines(Paths.get(pathTemp));
                }
            } catch (Exception e) {
                e.printStackTrace();
        }
        return null;
    }

However "sup" never gets printed to the console.

What I have tried

I thought it might be because the Application doesn't close, so I tried using:

    primaryStage.close();
    PlatformImpl.tkExit();
    Platform.exit();

But we can't use that since it produces an IllegalStateException: Toolkit has exited.

What I don't want to do

I don't wanna wrap all my code inside JavaFX since I only need it for such a small task.

If you want to take a closer look at the project in question: http://github.com/nymann/DeckSniffer/

Akudo
  • 165
  • 1
  • 11
  • 1
    See the answer to [How to call launch() more than once in java](http://stackoverflow.com/questions/24320014/how-to-call-launch-more-than-once-in-java). Does that help solve your issue? – jewelsea May 09 '16 at 22:29
  • Thanks for trying to help to my broad question, if you are interested in what solved my problem you can see my added "answer"/explanation :) – Akudo May 10 '16 at 02:52

1 Answers1

1

Brief explanation

It turns out that in order to just have a FileChooser popup window, you still need to primaryStage.show(); and primaryStage.close(); to stop it from halting. I then made a hackish way of making the primaryStage not briefly popping up by setting it's opacity to 0.

Working example

So what I ended up doing in the class that extends Application:

public class test1 extends Application{
    public static String fileAsString;

    @Override
    public void start(Stage primaryStage) throws Exception {
        FileChooser fileChooser = new FileChooser();
        File file = fileChooser.showOpenDialog(primaryStage);
        fileAsString = file.getAbsolutePath();

        primaryStage.setOpacity(0);
        primaryStage.show();
        primaryStage.close();
    }
}

And in the class where the Application get called from:

public class test2 {

    public test2() {
        testFromMethod();
    }

    public static void main(String[] args) {
        new test2();
    }

    private void testFromMethod() {
        System.out.println("Hello from testFromMethod()!");
        Application.launch(test1.class);
        System.out.println("Goodbye from testFromMethod()!");
        System.out.println(test1.fileAsString);
    }
}
Akudo
  • 165
  • 1
  • 11