Swing/IconTextField のバックアップ(No.14)
- バックアップ一覧
 - 差分 を表示
 - 現在との差分 を表示
 - 現在との差分 - Visual を表示
 - ソース を表示
 - Swing/IconTextField へ行く。
  
- 1 (2005-01-02 (日) 17:14:18)
 - 2 (2005-01-02 (日) 17:27:34)
 - 3 (2005-01-05 (水) 00:24:43)
 - 4 (2005-04-28 (木) 04:33:07)
 - 5 (2005-06-10 (金) 06:38:12)
 - 6 (2005-10-19 (水) 20:03:36)
 - 7 (2006-02-27 (月) 16:03:23)
 - 8 (2007-03-02 (金) 16:18:33)
 - 9 (2007-03-12 (月) 15:23:30)
 - 10 (2007-05-21 (月) 15:34:48)
 - 11 (2007-10-06 (土) 21:32:59)
 - 12 (2008-01-25 (金) 16:25:16)
 - 13 (2011-10-28 (金) 15:33:28)
 - 14 (2012-05-10 (木) 10:07:34)
 - 15 (2012-05-10 (木) 13:51:46)
 - 16 (2013-03-31 (日) 20:13:51)
 - 17 (2013-08-24 (土) 22:46:10)
 - 18 (2014-11-25 (火) 03:03:31)
 - 19 (2014-11-28 (金) 00:49:38)
 - 20 (2015-03-20 (金) 15:22:04)
 - 21 (2016-05-26 (木) 15:02:47)
 - 22 (2016-09-27 (火) 17:15:05)
 - 23 (2017-04-07 (金) 13:51:51)
 - 24 (2017-11-07 (火) 13:50:43)
 - 25 (2019-06-11 (火) 19:49:09)
 - 26 (2019-10-15 (火) 17:35:50)
 - 27 (2021-05-21 (金) 03:22:02)
 - 28 (2025-01-03 (金) 08:57:02)
 - 29 (2025-01-03 (金) 09:01:23)
 - 30 (2025-01-03 (金) 09:02:38)
 - 31 (2025-01-03 (金) 09:03:21)
 - 32 (2025-01-03 (金) 09:04:02)
 - 33 (2025-06-19 (木) 12:41:37)
 - 34 (2025-06-19 (木) 12:43:47)
 
 
TITLE:JTextField内にアイコンを追加
Posted by aterai at 2005-01-03
JTextField内にアイコンを追加
JTextFieldの内部にアイコンを表示します。
- &jnlp;
 - &jar;
 - &zip;
 
サンプルコード
ImageIcon image = new ImageIcon(getClass().getResource("16x16.png"));
int w = image.getIconWidth();
int h = image.getIconHeight();
JTextField field = new JTextField("bbbbbbbbbb");
Insets m = field.getMargin();
field.setMargin(new Insets(m.top, m.left+w, m.bottom, m.right));
JLabel label = new JLabel(image);
label.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
label.setBorder(null);
label.setBounds(m.left, m.top, w, h);
field.add(label);
解説
サンプルではsetMarginでJTextFieldの左に余白を作り、そこにJLabelを配置することでアイコン(画像)を表示しています。
JComboBoxにアイコンを追加のように、Borderを使っても同様のことができますが、JTextComponentを継承したコンポーネントでは、setMarginを使用するとカーソルの指定などが簡単にできるのでおすすめです。
また、JLabelの代わりに、JButtonなどを置くこともできます(参考リンクを参照)。
JComboBoxのEditorを取得してMarginを指定しても、なぜかうまくいきません。
JTextField field = (JTextField) combo02.getEditor().getEditorComponent();
JTextFieldの右端にJLabelを置く場合は、以下のようにSpringLayoutを使用する方法があります。
- JButtonなどの高さを変更せずに幅を指定
final JLabel label2 = new JLabel(image); label2.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); label2.setBorder(BorderFactory.createEmptyBorder()); JTextField field2 = new JTextField("ccccccccccccccccccccccccccc") { @Override public void updateUI() { super.updateUI(); removeAll(); SpringLayout l = new SpringLayout(); setLayout(l); Spring fw = l.getConstraint(SpringLayout.WIDTH, this); Spring fh = l.getConstraint(SpringLayout.HEIGHT, this); SpringLayout.Constraints c = l.getConstraints(label2); c.setConstraint(SpringLayout.WEST, fw); c.setConstraint(SpringLayout.SOUTH, fh); add(label2); } }; m = field2.getMargin(); field2.setMargin(new Insets(m.top+2,m.left,m.bottom,m.right+w)); 
参考リンク
- Swing (Archive) - Add a clickable icon to the left corner of a JTextField
 - JTextFieldのMarginを設定する
 - JComboBoxにアイコンを追加