以编程方式填充JPasswordField而不创建String对象

时间:2014-11-17 14:48:52

标签: java swing jpasswordfield

是否有一种简单的方法来填充JPasswordField的文档而不创建包含密码的String对象?

我试图创建一个"更改密码"该对话框接收旧密码并要求输入新密码两次(三个密码字段),其中旧密码可以事先知道,具体取决于用户如何配置(密码可能已存储)。因此,每次向她显示相关对话框时,不要求用户输入现有密码,而是希望以编程方式填写它。

请注意,JPasswordField.setText(String)String构造函数不是一个选项。我想使用char数组执行此操作。

我一直试图滥用似乎GapContent使用的PlainDocument,但它似乎没有效果(字符在那里但是字段已损坏) ):

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.text.PlainDocument;
import javax.swing.SwingUtilities;
import javax.swing.text.GapContent;

public class FillJPasswordField extends JFrame {

    private JPasswordField pass;

    public FillJPasswordField() {
        setLayout(new GridBagLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        char[] password = new char[]{'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
        PlainDocument doc = new PlainDocument(new MyGapContent(password));

        pass = new JPasswordField(doc, null, 20);

        // see if the password is in there
        System.out.println(new String(pass.getPassword()));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0d;
        gbc.insets = new Insets(10, 5, 10, 5);

        add(pass, gbc);

        pack();
        setLocationRelativeTo(null);
    }

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

            public void run() {
                new FillJPasswordField().setVisible(true);
            }
        });
    }

    private class MyGapContent extends GapContent {

        public MyGapContent() {
            super();
        }

        public MyGapContent(int initialLength) {
            super(initialLength);
        }

        public MyGapContent(char[] content) {
            this(content.length);
            replace(0, 0, content, content.length);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

嗯......以下似乎有效:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.GapContent;
import javax.swing.text.PlainDocument;

public class FillJPasswordField extends JFrame {

    private JPasswordField pass;

    public FillJPasswordField() {
        setLayout(new GridBagLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        char[] password = new char[]{'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};

        MyGapContent content = new MyGapContent();
        PlainDocument doc = new PlainDocument(content);
        try {
            content.insertChars(0, password);
        } catch (BadLocationException ex) {
        }
        pass = new JPasswordField(20);
        pass.setDocument(doc);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.PAGE_START;
        gbc.weightx = 1.0d;
        gbc.insets = new Insets(10, 5, 10, 5);
        add(pass, gbc);

        System.out.println(new String(pass.getPassword()));

        pack();
        setLocationRelativeTo(null);
    }

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

            public void run() {
                new FillJPasswordField().setVisible(true);
            }
        });
    }

    private class MyGapContent extends GapContent {

        public MyGapContent() {
            super();
        }

        public MyGapContent(int initialLength) {
            super(initialLength);
        }

        public void insertChars(int where, char[] chars) throws BadLocationException {
            if (where > length() || where < 0) {
                throw new BadLocationException("Invalid insert", length());
            }
            replace(where, 0, chars, chars.length);
        }
    }
}

以前的代码似乎并不喜欢我在replace的构造函数中调用MyGapContent的事实。也许它需要按某种顺序完成操作。