没有EventQueue的invokeLater,第一个UserTypedString没有在keyTyped(keyListener)中得到认可

时间:2022-10-21 00:19:11

This piece of code works fyn. But if I remove EventQueue.invokeLater() , first key that has been typed is not getting recoganised

这段代码适用于fyn。但是如果我删除了EventQueue.invokeLater(),那么键入的第一个键就不会被重新识别

   public static class ListenerDemo implements KeyListener
    {
    String userString = null;
    private String getUserTypedString(KeyEvent e)
    {
        return ((JTextField) e.getSource()).getText();
    }

    @Override
    public void keyTyped(final KeyEvent e) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                userString = ((JTextField) e.getSource()).getText();
                System.out.println("User str = " +userString);


            }
        });
    }

Input in JCombox : 1 , In keyTyped output is User str = 1

在JCombox中输入:1,在keyTyped输出中是User str = 1

But, If I dont use EventQueue.invokeLater()

但是,如果我不使用EventQueue.invokeLater()

Input in Jcombobox which I type is 1 , but output is userTyped =

我键入的Jcombobox输入为1,但输出为userTyped =

Then on typing 2, I get output as userTyped = 1

然后在输入2时,我得到userTyped = 1的输出

If all the events are handled by EDT thread , then is it mandatory to use EventQueue.invokeLater() . Why keyType is getting recoganised late?

如果所有事件都由EDT线程处理,则必须使用EventQueue.invokeLater()。为什么keyType迟到了?

1 个解决方案

#1


1  

Here is what I guess:
The value of JTextField is updated after the event being fired. So when you read text value in keyTyped method without invokeLater, you can not see the updated value.
When you type a key, two operations are done in the EDT:

我猜是这样的:JTextField的值在事件被触发后更新。因此,当您在没有invokeLater的情况下读取keyTyped方法中的文本值时,您无法看到更新的值。键入键时,在EDT中完成两项操作:

  1. fire keyTyped event by calling your method keyTyped(final KeyEvent e) (where you print the text).
  2. 通过调用方法keyTyped(final KeyEvent e)(打印文本的位置)来触发keyTyped事件。
  3. update text value.
  4. 更新文本值。
But if you schedule a Runnable with invokeLater, Swing adds this action at the end of the EDT queue, so it becomes:
  1. fire keyTyped event (where you schedule a Runnable)
  2. fire keyTyped事件(在其中安排Runnable)
  3. update text value
  4. 更新文本值
  5. print the text when Runnable gets called
  6. 调用Runnable时打印文本

#1


1  

Here is what I guess:
The value of JTextField is updated after the event being fired. So when you read text value in keyTyped method without invokeLater, you can not see the updated value.
When you type a key, two operations are done in the EDT:

我猜是这样的:JTextField的值在事件被触发后更新。因此,当您在没有invokeLater的情况下读取keyTyped方法中的文本值时,您无法看到更新的值。键入键时,在EDT中完成两项操作:

  1. fire keyTyped event by calling your method keyTyped(final KeyEvent e) (where you print the text).
  2. 通过调用方法keyTyped(final KeyEvent e)(打印文本的位置)来触发keyTyped事件。
  3. update text value.
  4. 更新文本值。
But if you schedule a Runnable with invokeLater, Swing adds this action at the end of the EDT queue, so it becomes:
  1. fire keyTyped event (where you schedule a Runnable)
  2. fire keyTyped事件(在其中安排Runnable)
  3. update text value
  4. 更新文本值
  5. print the text when Runnable gets called
  6. 调用Runnable时打印文本