Swing/GhostText のバックアップ差分(No.13)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/GhostText へ行く。
- 1 (2005-11-07 (月) 16:50:50)
- 2 (2006-02-27 (月) 15:57:52)
- 3 (2006-06-21 (水) 13:47:28)
- 4 (2006-11-10 (金) 12:57:38)
- 5 (2007-01-11 (木) 12:24:15)
- 6 (2007-09-14 (金) 21:09:25)
- 7 (2009-10-26 (月) 09:25:14)
- 8 (2009-10-26 (月) 13:06:20)
- 9 (2009-11-17 (火) 14:30:43)
- 10 (2009-11-17 (火) 15:48:18)
- 11 (2012-06-01 (金) 16:28:47)
- 12 (2012-06-04 (月) 14:30:56)
- 13 (2013-03-21 (木) 16:07:57)
- 14 (2013-10-16 (水) 14:10:12)
- 15 (2014-07-04 (金) 19:56:32)
- 16 (2014-07-26 (土) 04:48:51)
- 17 (2014-11-06 (木) 01:05:00)
- 18 (2015-12-04 (金) 18:12:07)
- 19 (2017-03-28 (火) 15:12:58)
- 20 (2018-02-01 (木) 19:34:47)
- 21 (2018-04-18 (水) 20:52:23)
- 22 (2018-10-17 (水) 18:42:18)
- 23 (2020-10-16 (金) 19:15:12)
- 24 (2022-07-29 (金) 11:33:02)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTextFieldにフォーカスと文字列が無い場合の表示 #navi(../) #tags() RIGHT:Posted by &author(aterai); at 2005-11-07 *JTextFieldにフォーカスと文字列が無い場合の表示 [#m17af070] JTextFieldにフォーカスが無く文字列が空の場合、薄い色でその説明を表示します。 -&jnlp; -&jar; -&zip; //#screenshot #ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTNY3BG1nI/AAAAAAAAAas/YJB5L9kNK-c/s800/GhostText.png) **サンプルコード [#lf2627f8] #code{{ #code(link){{ class HintTextFocusListener implements FocusListener { Color INACTIVE_COLOR = UIManager.getColor("TextField.inactiveForeground"); Color ORIGINAL_COLOR = UIManager.getColor("TextField.foreground"); private final String hintMessage; public HintTextFocusListener(final JTextComponent tf) { hintMessage = tf.getText(); tf.setForeground(INACTIVE_COLOR); } public void focusGained(final FocusEvent e) { JTextComponent textField = (JTextComponent)e.getSource(); String str = textField.getText(); Color col = textField.getForeground(); if(hintMessage.equals(str) && INACTIVE_COLOR.equals(col)) { textField.setForeground(ORIGINAL_COLOR); textField.setText(""); } } public void focusLost(final FocusEvent e) { JTextComponent textField = (JTextComponent)e.getSource(); String str = textField.getText().trim(); if("".equals(str)) { textField.setForeground(INACTIVE_COLOR); textField.setText(hintMessage); } } } }} **解説 [#r832c3b6] 上記のサンプルでは、下のJTextFieldからフォーカスが失われた時、まだ何も入力されていない場合は、そのJTextFieldの説明などを薄く表示することができるようにしています。 **参考リンク [#o0c01026] -[[JTextFieldに透かし画像を表示する>Swing/WatermarkInTextField]] -[[JPasswordFieldにヒント文字列を描画する>Swing/InputHintPasswordField]] -- JPasswordField の場合は、setTextなどが使えないので、透かし画像と同じ要領でpaintComponentをオーバーライドして文字列を描画する方法を使います。 **コメント [#h0951933] - タイトルを変更?: Input Hint、Placeholder、Watermark ... -- [[aterai]] &new{2009-11-17 (火) 15:48:18}; #comment