How to redirect all console output to a GUI textbox?

后端 未结 2 1965
无人及你
无人及你 2020-12-08 03:20

I currently have a program that prints lines of text to the screen in various manners such as \'System.out.println()\' statements and for loops the print all elements in an

相关标签:
2条回答
  • 2020-12-08 04:02

    An idea:

    Create your own PrintStream that outputs everything to this textbox. Then set this new PrintStream to be the standard output stream like that:

    System.setOut(myPrintStream());
    
    0 讨论(0)
  • 2020-12-08 04:12

    Check out this blog article, entitled Redirecting System.out and System.err to JTextPane or JTextArea. It describes almost everything you need.

    The basic idea is that you create your own specialized output stream. In your implementation of the write() methods, you call some code to append the new data to your text box. Then, you set this new output stream as your System.out by calling System.setOut() or System.setErr().

    NOTE: that article is missing one thing. You need to start your program in a separate thread.

    0 讨论(0)
提交回复
热议问题