I want to have a JTextArea that can completely work instead of a console but I don’t know how to do this!
Thank you
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The solution to the problem is to redirect
System.{in,out,err}to aJTextArea.Starting with
System.outit’s pretty straight forward to to redirect it to yourJTextAreacomponent usingSystem.setOutmethod. In the example below I’ve done this using pipes andSwingWorkerbut that is all fancy stuff to actually get the output simpler to the swing component.Emulating
System.inis simular, you need to redirect your keystrokes to the theSystem.inusingSystem.setIn. Again, in the example below, I’ve used pipes to get a nicer interface. I also buffer the line (just like a “normal” console would do) till you hit enter. (Note that for example arrow keys will not work but it shouldn’t be to much work to get it also to be handled/ignored.)The text in the screenshot below was produced by a number of calls to the “normal”
System.out.print..method and then waiting for input onSystem.inusing aScanner:And the example
mainmethod: