0

I have been working on a project and in terms of functionality it works completely but I just have it printing into the console. I want to print it in a new window with JFrame so that I can make a jar file out of it and have it run without needing the ide. How can i add JFrame or another tool to existing code to make it print text in a new window?

JoshuaR
  • 1
  • 1
  • 3
    FYI: JavaFX and `JFrame` are two different technologies, while the suggested duplicate works for Swing, it should be (more or less) easily modified to run with JavaFX. Ultimately, a better solution is to make use of an appropriate logging API – MadProgrammer Jun 25 '18 at 04:21

1 Answers1

1

Swing...

Is implemented in How to set output stream to TextArea

JavaFX

A "very basic" example of the above implementation.

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Date;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Test extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextArea textArea = new TextArea();

        PrintStream ps = System.out;
        System.setOut(new PrintStream(new StreamCapturer("STDOUT", new Consumer() {
            @Override
            public void appendText(String text) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        textArea.appendText(text);
                    }
                });
            }
        }, ps)));

        BorderPane root = new BorderPane(textArea);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Just going to output the current time");
                try {
                    while (true) {
                        Thread.sleep(1000);
                        System.out.println(new Date());
                    }
                } catch (InterruptedException ex) {
                }
            }
        });
        t.setDaemon(true);
        t.start();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    public interface Consumer {

        public void appendText(String text);
    }

    public class StreamCapturer extends OutputStream {

        private StringBuilder buffer;
        private String prefix;
        private Consumer consumer;
        private PrintStream old;

        public StreamCapturer(String prefix, Consumer consumer, PrintStream old) {
            this.prefix = prefix;
            buffer = new StringBuilder(128);
            buffer.append("[").append(prefix).append("] ");
            this.old = old;
            this.consumer = consumer;
        }

        @Override
        public void write(int b) throws IOException {
            char c = (char) b;
            String value = Character.toString(c);
            buffer.append(value);
            if (value.equals("\n")) {
                consumer.appendText(buffer.toString());
                buffer.delete(0, buffer.length());
                buffer.append("[").append(prefix).append("] ");
            }
            old.print(c);
        }
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366