是否有可能在JTextfield上引入KeyEvent?

时间:2012-05-25 15:30:41

标签: java swing inputstream jtextfield keyevent

这是我的问题,我得到的System.in被重定向到JTextField。现在,用户可以按Enter键,它将发送我的文本。但我的客户端将无法访问此JTextfield,因此我想知道是否可以在我的代码中重新创建Enter键。

   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();
    }
}
}

如果您需要更多信息,请在评论中提问! 谢谢你的帮助

P.S。:我确实尝试将动作侦听器更改为文档侦听器,但它并不总是触发事件,所以它没有像我想的那样行动。

尝试使用机器人,但似乎文本字段没有得到焦点所以关键只是按下而没有任何反应

//************************************************************************
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("");
}
//************************************************************************  

1 个答案:

答案 0 :(得分:2)

不知道这是否会有所帮助。但是你尝试过机器人课吗?

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);

将模拟Enter键的按键。

这有帮助吗?