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

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

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

请注意, JPasswordField.setText(String)String构造函数不是一个选项。 我想用char数组做这个。

我一直试图滥用似乎由PlainDocument使用的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); } } } 

嗯..以下似乎工作:

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

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