[JTextField] entrée standard dans un JTextField
Bonjour,
Je souhaiterai faire passer l'entrée standard par un JTexField. J'ai réussi à rediriger la sortie standard de cette façon:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
output = new JTextArea();
System.setOut(new PrintStream(new TextAreaOutputStream(output),true,"ISO-8859-1"));
...
private class TextAreaOutputStream extends OutputStream {
private JTextArea textArea;
public TextAreaOutputStream(JTextArea textArea){
super() ;
this.textArea = textArea;
}
@Override
public void write(int b) throws IOException {
textArea.append(new String(new byte[] { (byte) b }));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
textArea.append(new String(b, off, len));
}
} |
En revanche, cela ne marche pas pour l'entrée:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
input = new JTextField();
System.setIn(new TextFieldInputStream(input));
...
private class TextFieldInputStream extends InputStream {
private JTextField textField;
public TextFieldInputStream(JTextField textField) {
super() ;
this.textField = textField;
}
@Override
public int read() throws IOException {
java.io.StringReader sr = new java.io.StringReader(textField.getText());
return sr.read();
}
} |
Merci d'avance pour votre aide!