以编程方式填充 JPasswordField 而不创建 String 对象 [英] Fill a JPasswordField programmatically without creating a String object

查看:24
本文介绍了以编程方式填充 JPasswordField 而不创建 String 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a simple way to fill the document of a JPasswordField without creating a String object that contains the password?

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

I was trying to create a "change password" dialog which takes in the old password and requires the new one to be entered twice (three password fields), where the old password may be known before hand, depending on how the user configured this (password may have been stored). So instead of requiring the user to enter the existing password each time the associated dialog is shown to her, I wanted to fill it out programmatically.

注意 JPasswordField.setText(String)String 构造函数不是一个选项.我想使用 char 数组来做到这一点.

Note that JPasswordField.setText(String) and the String constructor are not an option. I'd like to do this using a char array.

我一直试图滥用 GapContent 似乎被 PlainDocument 使用,但它似乎不起作用(字符在那里,但字段已损坏):

I've been trying to abuse GapContent which seems to be used by PlainDocument, but it doesn't seem to work (the chars are in there but the field is corrupted):

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

    }
}

推荐答案

嗯.. 以下似乎有效:

Hmm.. The following seems to work:

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 的构造函数中调用 replace 的事实.也许它需要按某种顺序进行操作.

Previous code didn't seem to like the fact that I was calling replace inside the constructor of MyGapContent. Perhaps it requires operations to be done in some sequence.

这篇关于以编程方式填充 JPasswordField 而不创建 String 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆