Here is my problem I got my System.in that is redirected to a JTextField. Right now the user can press enter and it will send my text away. But my client will not have access to this JTextfield so i wanted to know if i could recreate the Enter key in my code.
public static JTextField jtfEntree = new JTextField();
public static TexfFieldStreamer ts = new TexfFieldStreamer(jtfEntree);
System.setIn(ts);
jtfEntree.addActionListener(ts);
//************************************************************************
private void commandLaunch(String command)
//************************************************************************
{
jtfEntree.setText(command);
//here is where i want to fire the key Enter
}
//************************************************************************
class TexfFieldStreamer extends InputStream implements ActionListener{
private JTextField tf;
private String str = null;
private int pos = 0;
public TexfFieldStreamer(JTextField jtf) {
tf = jtf;
}
public int read() {
//test if the available input has reached its end
//and the EOS should be returned
if(str != null && pos == str.length()){
str =null;
//this is supposed to return -1 on "end of stream"
//but I'm having a hard time locating the constant
return java.io.StreamTokenizer.TT_EOF;
}
//no input available, block until more is available because that's
//the behavior specified in the Javadocs
while (str == null || pos >= str.length()) {
try {
//according to the docs read() should block until new input is available
synchronized (this) {
this.wait();
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
//read an additional character, return it and increment the index
return str.charAt(pos++);
}
public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
str = tf.getText() + "\n";
pos = 0;
synchronized (this) {
//maybe this should only notify() as multiple threads may
//be waiting for input and they would now race for input
this.notify();
}
}
}
If you need more information ask in comments! thank you for your help
P.S.: I did try to change the action listener to a document listener but it doesnt always fire the event so it didnt act like i wanted to.
Tried with the Robot but it seems like the textfield isnt getting the focus so the key is just pressed and nothing happens
//************************************************************************
protected static void commandExecute(String Command) //COMMAND EXECUTE
//************************************************************************
{
jtfEntree.setText(Command);
jtfEntree.requestFocus();
Robot robot;
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jtfEntree.setText("");
}
//************************************************************************
Dont know if this would be of help. But did you try the robot class?
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
Would simulate a key press for Enter.
Does this help?