简体   繁体   English

jTable swing获取输入问题

[英]jTable swing get input problem

I've getInput method that takes string from text field on enter click. 我有getInput方法,它在输入click时从文本字段获取字符串。 I've created while loop to wait until onClickListener returns true (pressed Enter). 我创建了while循环,以等待onClickListener返回true(按Enter)。 Here's my code: 这是我的代码:

public String getInput(){
    jTextField1.setEditable(true);
    jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            temp=jTextField1KeyPressed(evt);
        };
    });
    while(!(temp)){
    }
    temp=false;
    jTextField1.setEditable(false);
    String s= jTextField1.getText();
    jTextField1.setText("");
    return s;

}

private boolean jTextField1KeyPressed(KeyEvent evt) {
    if (evt.getKeyCode() == KeyEvent.VK_ENTER)
        return true;
    else return false;
};

Now I have very strange problem: if I add System.out.println sentence in while loop, it works perfectly, if I remove it, while loop never exits. 现在,我遇到了一个非常奇怪的问题:如果我在while循环中添加System.out.println语句,它会完美运行,如果我删除它,则while循环永不退出。 Where is problem? 问题在哪里? Thanks in advance. 提前致谢。

You would need some synchronization so that the changes to temp are propagated to all required threads. 您将需要进行一些同步,以便将对temp的更改传播到所有必需的线程。 But it's a bad approach to start with - getInput will waste a lot of CPU looping in that while for no good purpose. 但这是一个不好的方法getInput会浪费很多CPU循环, while做没有任何目的。

You could do it more cleanly. 您可以做得更干净。 First, declare temp to be a java.lang.Object , and initialize it to a plain old Object: 首先,将temp声明为java.lang.Object ,并将其初始化为一个普通的旧Object:

private Object temp = new Object();

(or something like that.) (或类似的东西。)

In getInput, instead of this: 在getInput中,代替这个:

while (!temp) { }
temp = false;

put this: 把这个:

synchronized (temp) {
  try {
    temp.wait();
  } catch (InterruptedException ie) {
    // handle this situation: something interrupted your thread before input was finished
  }
}

And in the event handler: 在事件处理程序中:

synchronized (temp) {
  temp.notify();
}

This way, the thread running getInput stays asleep waiting for something to happen rather than burning CPU cycles. 这样,运行getInput的线程将保持睡眠状态,以等待发生某些事情,而不是消耗CPU周期。

There are also variants of wait() that take a timeout value. 也有采用超时值的wait()变体。 You might be interested in using those too. 您可能也对使用它们感兴趣。

Your problem is called "busy waiting". 您的问题称为“繁忙等待”。 You have to wait, until operating system tell you, that Enter was pressed. 您必须等待,直到操作系统告知您按下Enter键为止。 Asking a dummy question "Was Enter pressed?", "Was Enter pressed?", "Was Enter pressed?", "Was Enter pressed?", "Was Enter pressed?", "Was Enter pressed?", "Was Enter pressed?" 询问一个虚拟问题: “是否按下了Enter?”,“是否按下了Enter?”,“是否按下了Enter?”,“是否按下了Enter?”,“是否按下了Enter?”,“是否按下了Enter” ? 1000000000 times per second is not a good practice.... 每秒10亿次不是一个好习惯。

You may try to use this: 您可以尝试使用此方法:

jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyPressed(java.awt.event.KeyEvent evt) {
        temp=jTextField1KeyPressed(evt);
        if (temp==true) EnterPressed();
    };
});
void EnterPressed() {
    //enter code here
}

If you call your getInput from the event thread you will get stuck in an endless loop. 如果从事件线程调用getInput,则会陷入无限循环。 The variable temp might never get updated because no events can be processed anymore. 变量temp可能永远不会更新,因为无法再处理任何事件。

If you want to gather a plain input you might want to look into a solution like 如果您想收集简单的输入,则可能需要研究类似的解决方案

String info = JOptionPane.showInputDialog(this, "Please enter info");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM