【问题标题】:Calling a method everytime the text in JPasswordField is altered每次更改 JPasswordField 中的文本时调用方法
【发布时间】:2012-02-19 23:26:26
【问题描述】:

--编辑--
我有一个 JTextField,我希望每次有人更改(键入或删除)JTextField 中的一个字符时调用一个方法(暂时让它只是打印语句)。它背后的目的是让该方法立即检查键入的内容是否满足某些条件。感谢您的帮助,我设法写了:

public class MyDocumentListener implements DocumentListener {

    public void insertUpdate(DocumentEvent e) {
        updateLog(e, "inserted into");
    }
    public void removeUpdate(DocumentEvent e) {
        updateLog(e, "removed from");
    }
    public void changedUpdate(DocumentEvent e) {
        //Plain text components do not fire these events
    }

    public void updateLog(DocumentEvent e, String action) {
        System.out.println("should call the method here");
    }
}

JTextField 代码:

    JTextField passwordField = new JTextField();
    passwordField.getDocument().addDocumentListener(new MyDocumentListener());
    passwordField.getDocument().putProperty("name", "Text Field");

我现在遇到的问题是我需要使用

String textFieldPassword = passwordField.getText();

但它返回NullPointerException。我假设这是因为我添加了 DocumentListener 并且现在应该在 DocumentEvent 上运行。但我真的不知道该怎么做。

【问题讨论】:

  • 只需获取 Document 并向其添加 DocumentListener。不要使用 KeyListener,因为你不应该在 Swing 中使用它,它不会响应复制/粘贴操作。
  • 对。一个简单的问题:我可以使用passwordField.getText(); 并将其视为String 吗?
  • 不,不应该这样做。该方法已被弃用,因为您永远不想将密码视为字符串,因为字符串可能会持续存在,从而使您的密码易受攻击。最好将其视为字符数组。
  • “想要一个方法……在每次有人改变……一个字符时被调用” 为什么?什么方法?
  • 立即检查输入的内容是否满足特定条件的方法。

标签: java swing jpasswordfield


【解决方案1】:

你想要一个字段的动作监听器:

public class YourClass extends JPanel implements ActionListener {

    public void addComponents() {
        ...
        passwordField.addActionListener(this);
        ...
    }

    /**
      will be fired when the password field changes
    */
    public void actionPerformed(ActionEvent evt) {
        String text = passwordField.getText();
        System.out.println("key pressed");
    }
}

如果这不符合你的口味,那么你可以试试DocumentListener

【讨论】:

  • 没用。 public void actionPerformed(ActionEvent e) {String text = passwordField.getText(); System.out.println("key pressed, value is "+text); 第二行代码给出 NullPointerException - 在我按下“Enter”之后 - 只是在 JPasswordField 中输入字母没有任何效果。
  • -1 用于 ActionListener。 ActionEvent 仅在按下 Enter 键时触发,不满足要求。但是,为 DocumentListener +1。这是满足要求的解决方案。有关更多信息和示例,请参阅How to Write a Document Listener
【解决方案2】:

这就是我最终得到的结果(当 2 个密码匹配时启用按钮):

public class ChangePasswordUI implements DocumentListener, ActionListener {
    private JFrame frame;
    private JPasswordField newPassword1 = new JPasswordField(20);
    private JPasswordField newPassword2 = new JPasswordField(20);
    private JButton OKbutton;

    protected ChangePasswordUI() {
        OKbutton.addActionListener(this);
        newPassword1.addActionListener(this);
        newPassword2.addActionListener(this);
        newPassword1.getDocument().addDocumentListener(this);
        newPassword2.getDocument().addDocumentListener(this);

        frame = new JFrame();
        frame.add(newPassword1);
        frame.add(newPassword2);
        frame.pack();
        updateOKbutton();
        frame.setVisible(true);
    }

    private void updateOKbutton() {
        if(Arrays.equals(newPassword1.getPassword(),newPassword2.getPassword()) == false) {
            OKbutton.setEnabled(false);
        } else {
            OKbutton.setEnabled(true);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == cancelButton) {
            frame.dispose();
        } else if (e.getSource() == OKbutton) {
            frame.dispose();
        } else if (e.getSource() == newPassword1) {
            updateOKbutton();
        } else if (e.getSource() == newPassword2) {
            updateOKbutton();
        }
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        updateOKbutton();
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        updateOKbutton();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        updateOKbutton();
    }
}

注意事项:

  • 示例代码是我正在使用的“设置新密码”对话框的精简版,JFrame 的东西可能完全是狡猾的(我使用的是凌乱的GridBagLayout() 东西,没有人愿意看到那个)
  • .getText() 方法现在已被弃用(您希望使用 .getDocument())。
  • actionListener 对密码字段几乎没有用处(但可能需要用于其他事情,所以为什么不也包括在内!)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 2014-05-05
    • 2010-11-13
    相关资源
    最近更新 更多