7
\$\begingroup\$

I want to use JavaFX as a solution to Java's issues with buffered input making things like 'press any key to continue' not easy.

I plan to use an event listener to detect keyboard input. But before I start down that road I want to know if this is on the right track.

Is the code below a good way to use JavaFX to implement a shell-based program?

  • Is this the minimum JavaFX code required to do shell?
  • Am I missing something that's gonna bite me down the road (like some kind of object de-referencing: I am very new to JavaFX so I don't have a clue!)
  • Is it OK to consider the start method as effectively now my main method?

import java.util.Scanner;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;

public class TextBased extends Application {
    public static void main(String argv[]) {
        Application.launch(argv);
    }   

@Override
public void start(Stage primaryStage) {
    System.out.println("Hello world");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    Platform.exit();
    }   
}
\$\endgroup\$
1
  • \$\begingroup\$ JavaFX is mainly used with a GUI. You are simply overcomplicating your problem. The rest is the same as plain old Java. No de-referencing unless you do it manually; the GC is very smart about that. \$\endgroup\$
    – GiantTree
    Commented Dec 18, 2015 at 9:07

1 Answer 1

7
\$\begingroup\$

There's not a lot to review here, the only thing that's jumping at me is that this:

@Override
public void start(Stage primaryStage) {
    System.out.println("Hello world");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    Platform.exit();
    }

Should be indented like this:

@Override
public void start(Stage primaryStage) {
    System.out.println("Hello world");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    Platform.exit();
}

Is this the minimum JavaFX code required to do shell?

Well technically printing "Hello world" and waiting for the ENTER key is not needed if what you want is the minimum code. But then, what would your program be doing :-)

Am I missing something that's gonna bite me down the road (like some kind of object de-referencing: I am very new to JavaFX so I don't have a clue!)

I know nothing of JavaFX, but if garbage collection is anywhere like .net, you have nothing to worry about because AFAICT you're not creating any object that's holding on to a reference that would prevent its garbage collection.

Is it OK to consider the start method as effectively now my main method?

Probably. But it's not immediately clear what happens to argv.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.