简体   繁体   English

使用取消按钮时如何恢复InputVerifier机制?

[英]How to restore InputVerifier mechanism while using cancel button?

I have an InputVerifier on a JFormattedTextField. 我在JFormattedTextField上有一个InputVerifier。 It is called when field looses focus perfectly. 当视野完全失去焦点时被称为。 I added a cancel button (JButton), but do not want to call then InputVerifier when this button is clicked. 我添加了一个取消按钮(JButton),但是当单击此按钮时,不想调用InputVerifier。

MyVerifier is called when the focus is lost on the field without any problem. 当焦点毫无问题地丢失时,将调用MyVerifier。 However, after I click the clearButton the InputVerifier won´t be called again, as if the button where clicked even if it´s not. 但是,在我单击clearButton之后,不会再次调用InputVerifier,就好像单击了按钮一样,即使没有单击也是如此。

The complete code is: 完整的代码是:

public class DemoFrame extends javax.swing.JFrame {

    /** Creates new form DemoFrame */
    public DemoFrame() {
        initComponents();
        name.setValue("");
        name.setInputVerifier(new MyVerifier());
        clear.setVerifyInputWhenFocusTarget(false);
    }

    private class MyVerifier extends javax.swing.InputVerifier {
     public boolean verify(javax.swing.JComponent input) {
         System.out.println("Field is being verified");
          return true;
      }
      public boolean shouldYieldFocus(javax.swing.JComponent input) {
          return verify(input);
      }
  }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        name = new javax.swing.JFormattedTextField();
        clear = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        name.setText(" ");

        clear.setText("clear");
        clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(231, Short.MAX_VALUE)
                .addComponent(clear)
                .addGap(112, 112, 112))
            .addGroup(layout.createSequentialGroup()
                .addGap(116, 116, 116)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(156, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                .addComponent(clear)
                .addGap(73, 73, 73))
        );

        pack();
    }// </editor-fold>

    private void clearActionPerformed(java.awt.event.ActionEvent evt) {
        name.setValue(null);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DemoFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DemoFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DemoFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DemoFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DemoFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton clear;
    private javax.swing.JFormattedTextField name;
    // End of variables declaration
}

Why isn't the InputVerifier been invoked if the field looses focus without the button being clicked? 如果在没有单击按钮的情况下失去了焦点,为什么不调用InputVerifier?

Well. 好。 I found the solution. 我找到了解决方案。

    clear.setFocusable(false);

That solves the problem. 那解决了问题。

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

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