TITLE:JTextFieldにフォーカスと文字列が無い場合の表示
Posted by terai at 2005-11-07

JTextFieldにフォーカスと文字列が無い場合の表示

JTextFieldにフォーカスが無く文字列が空の場合、薄い色でその説明を表示します。
  • category: swing folder: GhostText title: JTextFieldにフォーカスと文字列が無い場合の表示 tags: [JTextField, Focus, FocusListener] author: aterai pubdate: 2005-11-07T16:50:50+09:00 description: JTextFieldにフォーカスが無く文字列が空の場合、薄い色でその説明を表示します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTNY3BG1nI/AAAAAAAAAas/YJB5L9kNK-c/s800/GhostText.png

概要

JTextFieldにフォーカスが無く文字列が空の場合、薄い色でその説明を表示します。

#screenshot

サンプルコード

#spanend
#spanadd
class PlaceholderFocusListener implements FocusListener {
#spanend
  private static final Color INACTIVE
    = UIManager.getColor("TextField.inactiveForeground");
  private final String hintMessage;

#spandel
**サンプルコード [#lf2627f8]
#spanend
#spandel
#code{{
#spanend
#spandel
class GhostFocusListener implements FocusListener {
#spanend
  private final String initMessage = "文字列を入力してください";
  private final Color dColor = UIManager.getColor("TextField.inactiveForeground");
  private final Color oColor = UIManager.getColor("TextField.foreground");
  public GhostFocusListener(final JTextComponent tf) {
    tf.setForeground(dColor);
    tf.setText(initMessage);
  public PlaceholderFocusListener(JTextComponent tf) {
    hintMessage = tf.getText();
    tf.setForeground(INACTIVE);
  }
  public void focusGained(final FocusEvent e) {
    JTextComponent textField = (JTextComponent)e.getSource();
    String str = textField.getText();
    Color col  = textField.getForeground();
    if(initMessage.equals(str) && dColor.equals(col)) {
      textField.setForeground(oColor);
      textField.setText("");
#spanadd

#spanend
  @Override public void focusGained(FocusEvent e) {
    JTextComponent tf = (JTextComponent) e.getComponent();
    if (hintMessage.equals(tf.getText())
        && INACTIVE.equals(tf.getForeground())) {
      tf.setForeground(UIManager.getColor("TextField.foreground"));
      tf.setText("");
    }
  }
  public void focusLost(final FocusEvent e) {
    JTextComponent textField = (JTextComponent)e.getSource();
    String str = textField.getText().trim();
    if("".equals(str)) {
      textField.setForeground(dColor);
      textField.setText(initMessage);
#spanadd

#spanend
  @Override public void focusLost(FocusEvent e) {
    JTextComponent tf = (JTextComponent) e.getComponent();
    if ("".equals(tf.getText().trim())) {
      tf.setForeground(INACTIVE);
      tf.setText(hintMessage);
    }
  }
}
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、下のJTextFieldからフォーカスが失われた時、まだ何も入力されていない場合は、そのJTextFieldの説明などを薄く表示することができるようにしています。

解説

上記のサンプルでは、JTextFieldからフォーカスが失われた時にまだ何も入力されていない場合はそのJTextFieldの説明などを透かし文字として薄く表示しています。

参考リンク

  • -
  • Java 1.7.0以上の場合JLayerを使用して同様にヒント文字列を描画可能
  • JTextFieldに透かし画像を表示する
    #spanend
    #spanadd
    class PlaceholderLayerUI extends LayerUI<JTextComponent> {
    #spanend
      private static final Color INACTIVE = UIManager.getColor("TextField.inactiveForeground");
      private final JLabel hint;
    
    #spandel
    **コメント [#h0951933]
    #spanend
      public PlaceholderLayerUI(String hintMessage) {
        super();
        this.hint = new JLabel(hintMessage);
        hint.setForeground(INACTIVE);
      }
    #spanadd
    
    #spanend
      @Override public void paint(Graphics g, JComponent c) {
        super.paint(g, c);
        if (c instanceof JLayer) {
          JLayer jlayer = (JLayer) c;
          JTextComponent tc = (JTextComponent) jlayer.getView();
          if (tc.getText().length() == 0 && !tc.hasFocus()) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setPaint(INACTIVE);
            Insets i = tc.getInsets();
            Dimension d = hint.getPreferredSize();
            SwingUtilities.paintComponent(g2, hint, tc, i.left, i.top, d.width, d.height);
            g2.dispose();
          }
        }
      }
    #spanadd
    
    #spanend
      @Override public void installUI(JComponent c) {
        super.installUI(c);
        if (c instanceof JLayer) {
          ((JLayer) c).setLayerEventMask(AWTEvent.FOCUS_EVENT_MASK);
        }
      }
    #spanadd
    
    #spanend
      @Override public void uninstallUI(JComponent c) {
        super.uninstallUI(c);
        if (c instanceof JLayer) {
          ((JLayer) c).setLayerEventMask(0);
        }
      }
    #spanadd
    
    #spanend
      @Override public void processFocusEvent(FocusEvent e, JLayer<? extends JTextComponent> l) {
        l.getView().repaint();
      }
    #spanadd
    }
    #spanend
    #spanadd
    

参考リンク

コメント