2

I'm looking for direction on how to replace System.in with an InputStream that reads directly from a JTextField.

So far my approach has pretty much been trial and error. I currently have;

JTextField input = new JTextField();

System.setIn(new InputStream() {
  int ptr = 0;
  @Override
  public int read() throws IOException {
     int c;
     try {
        c = input.getText().charAt(ptr);
     }
     catch (IndexOutOfBoundsException ioob) {
        return 0;
     }
     ptr++;
     return c;
  }
});

This yields an NoSuchElementException as in attempts to read when the input is empty and I assume can never find a delimiter.

What approach am I missing?

Jivings
  • 22,834
  • 6
  • 60
  • 101

2 Answers2

3

Well, this is the method that I used to get it working correctly. If anyone can improve this answer then feel free.

final LinkedBlockingQueue<Character> sb = new LinkedBlockingQueue<Character>();

final JTextField t = new JTextField();
t.addKeyListener(new KeyListener() {
  @Override
  public void keyTyped(KeyEvent e) {
    sb.offer(e.getKeyChar());
  }
  ...
});

System.setIn(new BufferedInputStream(new InputStream() {
  @Override
  public int read() throws IOException {
    int c = -1;
    try {
      c = sb.take();            
    } catch(InterruptedException ie) {
    } 
    return c;           
  }
}));
Jivings
  • 22,834
  • 6
  • 60
  • 101
  • You have made different semantic decisions than the specs indicate. And your not following the blocking contract. And return 0 // nothing to return right now, is just a bad decision in my eyes. – Captain Giraffe Jun 08 '12 at 21:20
  • @CaptainGiraffe I have improved upon my answer. – Jivings Jun 08 '12 at 21:26
  • @CaptainGiraffe I was incorrect in my 0 assumption. Apologies. – Jivings Jun 08 '12 at 21:27
  • Apologies are not necessary, but you still have the Cut/Paste scenario to consider. Also you could have made you LBQ yourself =) Isn't there an onChangeListener to simplify things for you? – Captain Giraffe Jun 08 '12 at 21:30
  • @CaptainGiraffe An on change listener would probably be better! I'll test and edit. Also I did do some stuff with locking first, then I remembered the LBQ =) – Jivings Jun 08 '12 at 21:34
1

You look halfway there, but:

From the Javadocs

This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

http://docs.oracle.com/javase/1.4.2/docs/api/java/io/InputStream.html#read%28%29

So your method should just wait for a key to be pressed. Either by handling NoSuchElementException, or a KeyListener that checks how many (new?) characters are available.

The semantics of this InputStream is different from a console one, so you need to make some design decisions on how to handle edits, not just keypresses.

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67