• title: JPasswordFieldでパスワードを可視化する tags: [JPasswordField, JCheckBox, JToggleButton, OverlayLayout, CardLayout] author: aterai pubdate: 2015-01-05T00:29:05+09:00 description: JPasswordFieldに入力したパスワードの表示・非表示を切り替えるためのボタンを作成し、これを入力欄などに配置します。 hreflang:
       href: http://java-swing-tips.blogspot.com/2015/01/showhide-passwordfield-using-cardlayout.html
       lang: en

概要

JPasswordFieldに入力したパスワードの表示・非表示を切り替えるためのボタンを作成し、これを入力欄などに配置します。

サンプルコード

JPasswordField pf = new JPasswordField(24);
pf.setText("abcdefghijklmn");
pf.setAlignmentX(Component.RIGHT_ALIGNMENT);
AbstractDocument doc = (AbstractDocument) pf.getDocument();
doc.setDocumentFilter(new ASCIIOnlyDocumentFilter());

AbstractButton b = new JToggleButton(new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    AbstractButton c = (AbstractButton) e.getSource();
    pf.setEchoChar(c.isSelected()
      ? '\u0000'
      : (Character) UIManager.get("PasswordField.echoChar"));
  }
});
b.setFocusable(false);
b.setOpaque(false);
b.setContentAreaFilled(false);
b.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 4));
b.setAlignmentX(Component.RIGHT_ALIGNMENT);
b.setAlignmentY(Component.CENTER_ALIGNMENT);
b.setIcon(new ColorIcon(Color.GREEN));
b.setRolloverIcon(new ColorIcon(Color.BLUE));
b.setSelectedIcon(new ColorIcon(Color.RED));
b.setRolloverSelectedIcon(new ColorIcon(Color.ORANGE));

JPanel panel = new JPanel() {
  @Override public boolean isOptimizedDrawingEnabled() {
    return false;
  }
};
panel.setLayout(new OverlayLayout(panel));
panel.add(b);
panel.add(pf);
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、JPasswordField#setEchoChar(...)メソッドの値に\u0000(0)を設定することで、パスワードを直接表示するように切り替えることのできるボタンを追加しています。

  • 上:
    • BorderLayoutで、JPasswordFieldの下にパスワード表示非表示切り替え用のJCheckBoxを配置
  • 中:
    • OverlayLayoutで、JPasswordField内の右端にパスワード表示非表示切り替え用のJToggleButtonを配置
    • JPasswordFieldJToggleButtonを配置するJPanelに、OverlayLayoutを設定し、z軸が入れ替わらないように、JPanel#isOptimizedDrawingEnabled()が常にfalseを返すようオーバーライド
  • 下:
    • 同じDocumentを使用するJPasswordFieldJTextFieldCardLayoutを設定したJPanelを作成
    • OverlayLayoutを設定したJPanelに、上記のJPanelJToggleButtonを配置し、JPasswordFieldなどの内部右端にボタンが表示されるように設定
    • JTextFieldをそのまま使用するので、表示中の文字列を選択してコピーすることが可能
JPasswordField pf3 = new JPasswordField(24);
pf3.setText("abcdefghijklmn");
AbstractDocument doc = (AbstractDocument) pf3.getDocument();
JTextField tf3 = new JTextField(24);
tf3.setFont(FONT);
tf3.enableInputMethods(false);
tf3.setDocument(doc);

final CardLayout cardLayout = new CardLayout();
final JPanel p3 = new JPanel(cardLayout);
p3.setAlignmentX(Component.RIGHT_ALIGNMENT);
p3.add(pf3, PasswordField.HIDE.toString());
p3.add(tf3, PasswordField.SHOW.toString());

AbstractButton b3 = new JToggleButton(new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    AbstractButton c = (AbstractButton) e.getSource();
    PasswordField s = c.isSelected() ? PasswordField.SHOW
                                     : PasswordField.HIDE;
    cardLayout.show(p3, s.toString());
  }
});

  • メモ: passwordField.setEchoChar((char) 0);を使用するサンプルを追加
    • JPasswordField#setEchoChar(...) (Java Platform SE 8)に、「値0に設定すると、標準のJTextFieldの動作と同様に、テキストが入力したとおりに表示されます。」とあるように、passwordField.setEchoChar((char) 0);とすれば、パスワードを表示することが可能
    • CardLayoutを使ってJTextFieldを表示する方法は一旦削除したが、表示のパスワードをコピーできるので、残しておくことにした

参考リンク

コメント