如何在分派任何更多事件之前等待JOptionPane关闭

时间:2012-06-18 21:36:43

标签: java swing override joptionpane event-dispatch-thread

我有几个文字字段,我正在其中。在focusLost()我正在打开JOptionPane。我希望focusGained()中的代码在JOptionPane关闭后执行。即使对话框是模态的,也会在focusGained()关闭之前调用JOptionPane。有没有办法解决?

发现了类似的问题,但它似乎也没有解决。 Postpone Event Queue after Focus Lost

这是一个代码示例。你会注意到在关闭JOptionPane之前会打印出“Focus Gained”。

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ShortTest implements FocusListener
{
private void go()
{
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    JTextField text1 = new JTextField();
    text1.setName("text1");
    text1.addFocusListener(this);

    JTextField text2 = new JTextField();
    text2.setName("text2");
    text2.addFocusListener(this);

    panel.add(new JLabel("tex1"));
    panel.add(text1);

    panel.add(new JLabel("text2"));
    panel.add(text2);

    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String [] args)
{
    ShortTest test = new ShortTest();
    test.go();
}

@Override
public void focusGained(FocusEvent e)
{
    if (!e.isTemporary() && (e.getSource() instanceof JTextField))
    {
        System.out.println("Focus Gained: " + ((JTextField)e.getSource()).getName());
    }
}

@Override
public void focusLost(FocusEvent e)
{
    if (!e.isTemporary() && (e.getSource() instanceof JTextField))
    {
        JOptionPane.showOptionDialog(null, ((JTextField)e.getSource()).getName() + " lost focus", "Title", JOptionPane.DEFAULT_OPTION, 0, null, null, null);
    }
}
}

1 个答案:

答案 0 :(得分:3)

也许你想要的不是一个焦点监听器(一个非常低级的构造),而是一个输入验证器(一个更高级别的构造)。这应该在焦点转移之前做出反应。例如,在下面的代码中,如果用户尝试在文本字段中输入非数字数据,则验证程序会做出反应。是的,这也可以使用DocumentFilter完成。

import javax.swing.*;

public class VerifierEg extends JPanel {
   private static final int FIELD_COUNT = 3;

   public VerifierEg() {
      InputVerifier inputVerifier = new InputVerifier() {

         @Override
         public boolean verify(JComponent input) {
            final JTextField textField = (JTextField) input;
            String text = textField.getText();
            for (char c : text.toCharArray()) {
               if (!Character.isDigit(c)) {
                  textField.setText("");
                  JOptionPane.showMessageDialog(VerifierEg.this, "Text: \""
                        + text + "\" must hold only digits", "Text Field Error",
                        JOptionPane.ERROR_MESSAGE);
                  return false;
               }
            }
            return true;
         }
      };
      for (int i = 0; i < FIELD_COUNT; i++) {
         JTextField field = new JTextField(6);

         field.setInputVerifier(inputVerifier);
         add(field);
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("Enter Numbers");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new VerifierEg());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

编辑:
即使您没有以任何特定方式验证输入,InputVerifier也可以用于您的目的。例如,要修改代码:

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.BoxLayout;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ShortTest2 {
   private void go() {
      final JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JPanel panel = new JPanel();
      panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

      InputVerifier inputVerifier = new InputVerifier() {

         @Override
         public boolean verify(JComponent input) {
            JOptionPane.showMessageDialog(frame,
                  "Focus Lost on " + input.getName());
            return true;
         }
      };

      FocusListener focusListener = new FocusListener() {

         @Override
         public void focusLost(FocusEvent e) {
            String name = ((JComponent)e.getSource()).getName();
            System.out.println("Focus Lost: " + name );
         }

         @Override
         public void focusGained(FocusEvent e) {
            String name = ((JComponent)e.getSource()).getName();
            System.out.println("Focus Gained: " + name );
         }
      };

      JTextField[] textFields = new JTextField[2];
      for (int i = 0; i < textFields.length; i++) {
         JTextField textField = new JTextField(10);
         String name = "text " + (i + 1);
         textField.setName(name);
         textField.setInputVerifier(inputVerifier);
         textField.addFocusListener(focusListener);

         panel.add(new JLabel(name));
         panel.add(textField);         
      }

      frame.setContentPane(panel);
      frame.pack();
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      ShortTest2 test = new ShortTest2();
      test.go();
   }
}
顺便提一下,你的SSCCE是1+!