0

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("");
}
//************************************************************************  
Alex
  • 865
  • 13
  • 24
  • 1
    *"my client will not have access to this JTextfield"* Why not? As with your last question, it would pay to describe the ultimate goal e.g. "provide a command line based tool to get exchange rates from a remote server"*. As it is, your questions sound like *"How do I use a screw-driver to install a nail?"* – Andrew Thompson May 26 '12 at 06:45

1 Answers1

2

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?

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
QVSJ
  • 1,165
  • 2
  • 12
  • 22
  • i added the Robot but it doesnt seem to fire the event.looks like the Enter is pressed but the actionEvent isnt thrown. – Alex May 25 '12 at 15:59
  • +1 It seems to work in this [example](http://stackoverflow.com/a/2596700/230513). – trashgod May 25 '12 at 18:11