开发者

redirect inputStream to JTextField

开发者 https://www.devze.com 2023-03-19 21:47 出处:网络
I want to redirect 开发者_StackOverflowthe Standard System input to JTextField, So that a user must type his/her input in JTextField (instead of console.)

I want to redirect 开发者_StackOverflowthe Standard System input to JTextField, So that a user must type his/her input in JTextField (instead of console.)

I found System.setIn(InputStream istream) for redirecting System.in.

Here is my scratch code where i confused on reading from JTextField - inputJTextField.

 System.setIn(new InputStream() {
     @Override
         public int read() throws IOException {
         //how to read content?
         return Integer.parseInt(inputJTextField.getText());
      }
  });

My Question is how to read content from GUI Component ( like JTextField and Cast it to String and other types after redirecting the input stream?


If you want to convert arbitrary string to InputStream, then use ByteArrayInputStream.

ByteArrayInputStream stringToInputStream ( final String s )
{
  // Assume your input is in UTF-8
  ByteArrayInputStream result =
    new ByteArrayInputStream( s.getBytes( "UTF-8" ) );

  return result;
}

However, I am not sure that your whole approach of setting your running process'es input stream will work.


for multilines intput to the Gui would be better to look for JEditorPane or JTextPane, lots of examples on this forum for JEditorPanes ot JTextPanes or here

0

精彩评论

暂无评论...
验证码 换一张
取 消