如何等待JOptionPane的调度更多的事件之前被关闭(How to wait for JOpti

2019-09-18 06:05发布

我有我之间的互联几个文本字段。 在focusLost()我开JOptionPane 。 我想在代码focusGained()后的被执行JOptionPane已经关闭。 即使对话框模式, focusGained()在之前被调用JOptionPane关闭。 有没有办法解决?

发现这个类似的问题,但它似乎并没有被任何解决。 推迟事件队列聚焦丢失后

下面是一个代码示例。 你会发现“专注虎子”是的JOptionPane关闭之前打印出来。

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

Answer 1:

也许你想要的是不是一个焦点监听器(一个非常低级别的结构),而是输入验证(更高级别的结构)。 焦点转移之前这应该作出回应。 例如,在验证以下的代码,如果用户试图输入非数字数据到文本字段中反应。 是的,这也可以使用某个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();
   }
}

1+您SSCCE的方式!



文章来源: How to wait for JOptionPane to be closed before dispatching any more events