Swing/IconTextField のバックアップの現在との差分(No.11)
TITLE:JTextField内にアイコンを追加
JTextField内にアイコンを追加
編集者:Terai Atsuhiro~
作成日:2005-01-03
更新日:2021-05-21 (金) 03:22:02
概要
JTextField
の内部に余白を生成し、そこにImageIcon
を設定したJLabel
を配置します。
Screenshot
Advertisement
概要
JTextFieldの内部にアイコンを表示します。サンプルコード
#spanend
#spanadd
ImageIcon image = new ImageIcon(getClass().getResource("16x16.png"));
#spanend
#spanadd
int w = image.getIconWidth();
#spanend
#spanadd
int h = image.getIconHeight();
#spanend
#spandel
#screenshot
#spanend
#spanadd
JTextField field = new JTextField("bbbbbbbbbb");
#spanend
#spanadd
Insets m = field.getMargin();
#spanend
#spanadd
field.setMargin(new Insets(m.top, m.left + w, m.bottom, m.right));
#spanend
#spandel
**サンプルコード [#rc7f0af2]
#spanend
#spandel
#code{{
#spanend
int w = image.getIconWidth();
int h = image.getIconHeight();
JLabel label = new JLabel(image);
label.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
label.setBorder(null);
label.setBounds(1,1,w,h);
Insets m = field.getMargin();
field.setMargin(new Insets(m.top,m.left+w,m.bottom,m.right));
field.add(label);
#spanadd
JLabel label = new JLabel(image);
#spanend
#spanadd
label.setCursor(Cursor.getDefaultCursor());
#spanend
#spanadd
label.setBorder(null);
#spanend
#spanadd
label.setBounds(m.left, m.top, w, h);
#spanend
#spanadd
#spanend
#spanadd
field.add(label);
#spanend
View in GitHub: Java, Kotlin- &jnlp;
- &jar;
- &zip;
解説
サンプルではsetMarginでJTextFieldの左に余白を作り、そこにJLabelを配置することでアイコン(画像)を表示しています。解説
サンプルではsetMargin
でJTextField
の左に余白を作り、そこにJLabel
を配置することでアイコン(画像)を表示しています。
JComboBoxにアイコンを追加のように、Borderを使っても同様のことができますが、JTextComponentを継承したコンポーネントでは、setMarginを使用するとカーソルの指定などが簡単にできるのでおすすめです。
- JComboBoxにアイコンを追加のように
Border
を使用する方法もあるが、JTextComponent
を継承するコンポーネントではsetMargin(...)
メソッドを使用するとカーソルの指定などが簡単に実現可能-
JLabel
の代わりにJButton
などのコンポーネントを配置することも可能
-
-
JComboBox
のEditor
を取得してMargin
を指定しても反映されない - JTextComponent#setMargin(...) (Java Platform SE 8)
ただし、デフォルト以外の境界が設定されている場合は、
Border
オブジェクトが適切なマージン空白を作成します(それ以外の場合、このプロパティーは事実上無視される)。
- -
-
JTextField
の右端にJLabel
を置く場合は、以下のようにSpringLayout
を使用する方法もある- JButtonなどの高さを変更せずに幅を指定
JTextField field = (JTextField) combo02.getEditor().getEditorComponent(); #spanadd final JLabel label2 = new JLabel(image); #spanend #spanadd label2.setCursor(Cursor.getDefaultCursor()); #spanend #spanadd label2.setBorder(BorderFactory.createEmptyBorder()); #spanend #spanadd JTextField field2 = new JTextField("ccccccccccccccccccccccccccc") { #spanend @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); } #spanadd }; #spanend #spanadd m = field2.getMargin(); #spanend #spanadd field2.setMargin(new Insets(m.top + 2, m.left, m.bottom, m.right + w)); #spanend
- JButtonなどの高さを変更せずに幅を指定
参考リンク
参考リンク
- Swing (Archive) - Add a clickable icon to the left corner of a JTextField
- java - JTextField margin doesnt work with border - Stack Overflow
- JTextFieldのMarginを設定する
- JComboBoxにアイコンを追加