How to make custom Java/JavaFX console?

2019-05-15 13:01发布

问题:

It's necessary to make a custom console. I have the following code:

public class Console extends OutputStream{

private Console(ResourceBundle resourceBundle) throws IOException {
    FXMLLoader loader = new FXMLLoader(this.getClass().getResource("Console.fxml"), resourceBundle);
    controller = loader.getController();
    Scene scene = new Scene((Parent) loader.load());
    stage = new Stage();
    stage.setScene(scene);
    show();
}

@Override
public void write(int b) throws IOException {
    controller.append(b);
}

public static Console getInstance(ResourceBundle resourceBundle) {
    if (console == null) {
        try {
            console = new Console(resourceBundle);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    return console;
}

public void show() {
    stage.show();
}

private static Console console = null;
private ConsoleController controller;
private Stage stage;
}

controller file:

public class ConsoleController implements Initializable {

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}

@FXML
public void append(int i) {
    textArea.appendText(String.valueOf((char) i));
}

@FXML
private TextArea textArea;
}

And all these are called from the 'start()':

    @Override
public void start(Stage primaryStage) throws IOException {
    ...
    System.setErr(new PrintStream(Console.getInstance(rb)));
}

During the exception nothing is printed in the textArea of the Console.fxml, but the following exception is thrown:

Exception in thread "JavaFX Application Thread" Exception: java.lang.NullPointerException thrown from the UncaughtExceptionHandler in thread "JavaFX Application Thread"

What am I doing wrong?

------EDIT--------

After understanding that it's necessary to use several threads I have the following code:

public class Console{
private Console(ResourceBundle resourceBundle) throws IOException {
    FXMLLoader loader = new FXMLLoader(this.getClass().getResource("Console.fxml"), resourceBundle);
    Parent root = (Parent) loader.load();
    controller = loader.getController();
    Scene scene = new Scene(root);
    stage = new Stage();
    stage.setScene(scene);
    show();

    if (errorOutputThread != null) {
        errorOutputThread.interrupt();
        try {
            errorOutputThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        errorOutputThread = null;
    }

    if (outOutputThread != null) {
        outOutputThread.interrupt();
        try {
            outOutputThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        outOutputThread = null;
    }

    System.err.flush();
    System.out.flush();

    outPipedInputStream = new PipedInputStream();
    outPipedOutputStream = new PipedOutputStream(outPipedInputStream);
    System.setOut(new PrintStream(outPipedOutputStream));

    errorPipedInputStream = new PipedInputStream();
    errorPipedOutputStream = new PipedOutputStream(errorPipedInputStream);
    System.setErr(new PrintStream(errorPipedOutputStream));

    outOutputThread = new Thread(new ConsoleStream(outPipedInputStream, "OUT"));
    outOutputThread.setDaemon(true);
    outOutputThread.start();

    errorOutputThread = new Thread(new ConsoleStream(errorPipedInputStream, "ERROR"));
    errorOutputThread.setDaemon(true);
    errorOutputThread.start();

    controller.appendText("Start Console");

}

public static Console getInstance(ResourceBundle resourceBundle) {
    if (console == null) {
        try {
            console = new Console(resourceBundle);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    return console;
}

public void show() {
    stage.show();
}

private class ConsoleStream implements Runnable {
    private ConsoleStream(InputStream in, String type) {
        inputStream = in;
        this.type = type;
    }

    public void run() {
        try {
            InputStreamReader is = new InputStreamReader(inputStream);
            BufferedReader br = new BufferedReader(is);
            String read = null;
            read = br.readLine();
            while(read != null) {
                controller.appendText(read + "\n");
                read = br.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        controller.appendText("Thread" + type + "started");

    }

    private final InputStream inputStream;
    private String type;
}

private static Console console = null;
private ConsoleController controller;
private Stage stage;
private PrintStream printStream;
private PipedOutputStream customPipedOutputStream;
private PipedOutputStream errorPipedOutputStream;
private PipedOutputStream outPipedOutputStream;
private PipedInputStream customPipedInputStream;
private PipedInputStream errorPipedInputStream;
private PipedInputStream outPipedInputStream;
private Thread customOutputThread;
private Thread outOutputThread;
private Thread errorOutputThread;
}

But the result is only: "Start Console", there are no results of controller.appendText("Thread" + type + "started"); so it seems that this threads don't start. But why?

回答1:

I think you have to load the form before you can get the controller instance

FXMLLoader loader = new FXMLLoader(this.getClass().getResource("Console.fxml"),resourceBundle);
Parent p = (Parent) loader.load()
controller = loader.getController();
Scene scene = new Scene(p);