Hey guys I am making a terminal application using Swing and Apache Commons. I was able to redirect System.out and System.err to a JTextArea easily but how do I do that for System.in ? Will I need to override Inputstream methods? Do I need to convert the String from JTextArea to byte array and then pass it to InputStream? Code examples would be nice.
Hey guys I am making a terminal application using Swing and Apache Commons. I
Share
I recently tried the same thing, here’s my code:
and use it like this:
Has been a while since I coded Java so I may not be up-to-date with the patterns. You should probably also overload
int available()but my example just consists of the bare minimum to make it work with aBufferedReadersreadLine()function.Edit: For this to work for a
JTextFieldyou have to useimplements KeyListenerinstead ofimplements ActionListenerand then useaddKeyListener(...)on your TextArea. The function that you need instead ofactionPerformed(...)ispublic void keyPressed(KeyEvent e)and then you have to test forif (e.getKeyCode() == e.VK_ENTER)and instead of using the whole text you just use a substring from the last line before the cursor withfor the input string. Because otherwise you would read the whole TextArea every time you press enter.