• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTextField内にアイコンを追加
#navi(../)
*JTextField内にアイコンを追加 [#h496664d]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2005-01-03~
更新日:&lastmod;
---
category: swing
folder: IconTextField
title: JTextField内にアイコンを追加
tags: [JTextField, ImageIcon, JLabel, Border]
author: aterai
pubdate: 2005-01-03T02:14:18+09:00
description: JTextFieldの内部に余白を生成し、そこにImageIconを設定したJLabelを配置します。
image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTOSatpfJI/AAAAAAAAAcI/9Ghfvb82FsM/s800/IconTextField.png
---
* 概要 [#summary]
`JTextField`の内部に余白を生成し、そこに`ImageIcon`を設定した`JLabel`を配置します。

#contents
#download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTOSatpfJI/AAAAAAAAAcI/9Ghfvb82FsM/s800/IconTextField.png)

**概要 [#xfd25592]
JTextFieldの内部にアイコンを表示します。
* サンプルコード [#sourcecode]
#code(link){{
ImageIcon image = new ImageIcon(getClass().getResource("16x16.png"));
int w = image.getIconWidth();
int h = image.getIconHeight();

#screenshot
JTextField field = new JTextField("bbbbbbbbbb");
Insets m = field.getMargin();
field.setMargin(new Insets(m.top, m.left + w, m.bottom, m.right));

**サンプルコード [#rc7f0af2]
 JLabel label = new JLabel(image);
 label.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
 label.setBorder(null);
 label.setBounds(1,1,image.getIconWidth(),image.getIconHeight());
 Insets m  = field01.getMargin();
 Insets m2 = new Insets(m.top,m.left+image.getIconWidth(),m.bottom,m.right);
 field01.setMargin(m2);
 field01.add(label);
JLabel label = new JLabel(image);
label.setCursor(Cursor.getDefaultCursor());
label.setBorder(null);
label.setBounds(m.left, m.top, w, h);

-&jnlp;
-&jar;
-&zip;
field.add(label);
}}

**解説 [#z9fc33ce]
サンプルではsetMarginでJTextFieldの左に余白を作り、そこにJLabelを配置することでアイコンを表示しています。
* 解説 [#explanation]
サンプルでは`setMargin`で`JTextField`の左に余白を作り、そこに`JLabel`を配置することでアイコン(画像)を表示しています。

[[JComboBoxにアイコンを追加>Swing/IconComboBox]]のように、Borderを使っても同様のことができますが、JTextComponentを継承したコンポーネントでは、setMarginを使用するとカーソルの指定などが簡単にできるのでおすすめです。
- [[JComboBoxにアイコンを追加>Swing/IconComboBox]]のように`Border`を使用する方法もあるが、`JTextComponent`を継承するコンポーネントでは`setMargin(...)`メソッドを使用するとカーソルの指定などが簡単に実現可能
-- `JLabel`の代わりに`JButton`などのコンポーネントを配置することも可能
- `JComboBox`の`Editor`を取得して`Margin`を指定しても反映されない
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/text/JTextComponent.html#setMargin-java.awt.Insets- JTextComponent#setMargin(...) (Java Platform SE 8)]
> ただし、デフォルト以外の境界が設定されている場合は、`Border`オブジェクトが適切なマージン空白を作成します(それ以外の場合、このプロパティーは事実上無視される)。

また、JLabelの代わりに、JButtonなどを置くこともできます(参考リンクを参照)。
----
- `JTextField`の右端に`JLabel`を置く場合は、以下のように`SpringLayout`を使用する方法もある
-- [[JButtonなどの高さを変更せずに幅を指定>Swing/ButtonWidth]]
#code{{
final JLabel label2 = new JLabel(image);
label2.setCursor(Cursor.getDefaultCursor());
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));
}}

JComboBoxのEditorを取得してMarginを指定しても、なぜかうまくいきません。
 JTextField field = (JTextField) combo02.getEditor().getEditorComponent();
* 参考リンク [#reference]
- [https://community.oracle.com/thread/1489851 Swing (Archive) - Add a clickable icon to the left corner of a JTextField]
- [https://stackoverflow.com/questions/10496828/jtextfield-margin-doesnt-work-with-border java - JTextField margin doesnt work with border - Stack Overflow]
- [[JTextFieldのMarginを設定する>Swing/TextFieldMargin]]
- [[JComboBoxにアイコンを追加>Swing/IconComboBox]]

**参考リンク [#g5b06829]
-[[Java Forums - Add a clickable icon to the left corner of a JTextField>http://forum.java.sun.com/thread.jspa?forumID=57&threadID=454437]]
-[[JTextFieldのMarginを設定する>Swing/TextFieldMargin]]
-[[JComboBoxにアイコンを追加>Swing/IconComboBox]]

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