Java 每次更改JPasswordField中的文本时调用方法

Java 每次更改JPasswordField中的文本时调用方法,java,swing,jpasswordfield,Java,Swing,Jpasswordfield,--编辑-- 我有一个JTextField,我想在每次有人更改(键入或删除)JTextField中的字符时调用一个方法(现在就让它成为print语句)。它背后的目的是让方法立即检查键入的内容是否满足某些条件。多亏了你的帮助,我终于写了: public class MyDocumentListener implements DocumentListener { public void insertUpdate(DocumentEvent e) { updateLog(e,

--编辑--
我有一个JTextField,我想在每次有人更改(键入或删除)JTextField中的字符时调用一个方法(现在就让它成为print语句)。它背后的目的是让方法立即检查键入的内容是否满足某些条件。多亏了你的帮助,我终于写了:

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
进行操作。但是我真的不知道怎么做。

你想要一个现场的动作监听器:

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

如果这不符合您的口味,那么您可以尝试。

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

注:

  • 示例代码是我正在使用的“设置新密码”对话框的精简版本,
    JFrame
    内容可能完全不可靠(我正在使用一个凌乱的
    GridBagLayout()
    东西,没有人想看到)
  • .getText()
    方法现在已被弃用(您可能希望使用
    .getDocument()
  • actionListener
    对于密码字段来说基本上是无用的(但对于其他东西可能是必需的,所以为什么不把它也包括进去呢!)

只需获取文档并向其添加DocumentListener即可。不要使用KeyListener,因为您不应该在Swing中使用它,而且它不会响应复制/粘贴操作。对。只需要一个简单的问题:我可以使用
passwordField.getText()吗
并将其视为
字符串
?不,不应该这样做。此方法已被弃用,因为您永远不希望将密码视为字符串,因为字符串可能会持续存在,从而使您的密码易受攻击。最好将其视为字符数组。“想要一个方法..在每次有人更改时调用..一个字符”为什么?什么方法?立即检查键入的内容是否满足特定条件的方法。不起作用
public void actionPerformed(ActionEvent e)
{String text=passwordField.getText();
System.out.println(“按下键,值为“+text”);
第二行代码给出NullPointerException-在我按下“Enter”之后-仅在JPasswordField中键入字母没有任何效果。-1用于ActionListener。ActionEvent仅在按下Enter键时触发,这不满足要求。但是,+1用于DocumentListener。这是满足要求的解决方案。有关更多信息和示例,请参阅。
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();
    }
}