• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JPasswordFieldのエコー文字を変更
#navi(../)
RIGHT:Posted by &author(aterai); at 2006-12-11
*JPasswordFieldのエコー文字を変更 [#x1cc913a]
JPasswordFieldのエコー文字を独自の図形に変更します。
---
category: swing
folder: PasswordView
title: JPasswordFieldのエコー文字を変更
tags: [JPasswordField, Icon]
author: aterai
pubdate: 2006-12-11T14:51:59+09:00
description: JPasswordFieldのエコー文字を独自のIcon図形に変更します。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTQ8cs8ApI/AAAAAAAAAgY/gxUUdKI65yA/s800/PasswordView.png
---
* 概要 [#summary]
`JPasswordField`のエコー文字を独自の`Icon`図形に変更します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTQ8cs8ApI/AAAAAAAAAgY/gxUUdKI65yA/s800/PasswordView.png)

//#screenshot
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTQ8cs8ApI/AAAAAAAAAgY/gxUUdKI65yA/s800/PasswordView.png)

**サンプルコード [#bd030635]
* サンプルコード [#sourcecode]
#code(link){{
class MyPasswordFieldUI extends BasicPasswordFieldUI {
  public View create(Element elem) {
  private static final StarIcon ICON = new StarIcon();
  public static MyPasswordFieldUI createUI(JPasswordField c) {
    c.setEchoChar('\u25A0'); // As wide as a CJK character cell (fullwidth)
    return new MyPasswordFieldUI();
  }

  @Override public View create(Element elem) {
    return new MyPasswordView(elem);
  }
  class MyPasswordView extends PasswordView{

  private static class MyPasswordView extends PasswordView {
    @Override protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
      FontMetrics fm = g.getFontMetrics();
      ICON.paintIcon(null, g, x, y - fm.getAscent());
      return x + ICON.getIconWidth(); // fm.charWidth(c);
    }

    public MyPasswordView(Element element) {
      super(element);
    }
    protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
      Graphics2D g2d = (Graphics2D) g;
      FontMetrics fm = g2d.getFontMetrics();
      icon.paintIcon(null, g, x, y-fm.getAscent());
      return x + icon.getIconWidth();

      //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
      //                                    RenderingHints.VALUE_ANTIALIAS_ON);
      //FontMetrics fm = g2d.getFontMetrics();
      //int r = fm.charWidth(c)-4;
      ////g2d.setPaint(Color.GRAY);
      //g2d.drawRect(x+2, y+4-fm.getAscent(), r, r);
      ////g2d.setPaint(Color.GRAY.brighter());
      //g2d.fillOval(x+2, y+4-fm.getAscent(), r, r);
      //return x + fm.charWidth(c);
    }
  }
}
}}

**解説 [#vc509445]
上記のサンプルでは、下のJPasswordFieldのエコー文字を独自の図形に変更しています。またこの図形のサイズを上のJPasswordFieldのエコー文字と幅を合わせるために同じ文字をsetEchoCharしています。
* 解説 [#explanation]
- 上: `setEchoChar('\u2605')`
-- `JPasswordField#setEchoChar(...)`メソッドで任意の文字をエコー文字に設定
- 下: `drawEchoCharacter`
-- `PasswordView#drawEchoCharacter(...)`をオーバーライドして任意の図形をエコー文字として描画する`BasicPasswordFieldUI`を作成し`JPasswordField`に設定
-- 上の`JPasswordField`のエコー文字と同じ文字を`setEchoChar`で設定し図形のサイズを合わせる

//**参考リンク
**コメント [#n2314028]
* 参考リンク [#reference]
- [[JPasswordFieldでパスワードを可視化する>Swing/ShowHidePasswordField]]
- [https://bugs.openjdk.org/browse/JDK-6852577 [JDK-6852577] Only for Nimbus LAF UIManager.get("PasswordField.echoChar") is null - Java Bug System]

* コメント [#comment]
#comment
#comment