TITLE:JTextField内にアイコンを追加
Posted by at 2005-01-03

JTextField内にアイコンを追加

JTextFieldの内部にアイコンを表示します。
  • 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

概要

JTextFieldの内部に余白を生成し、そこにImageIconを設定したJLabelを配置します。
IconTextField.png

サンプルコード

#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
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();
#spandel
field.setMargin(new Insets(m.top, m.left+w, m.bottom, m.right));
#spanend
#spanadd
field.setMargin(new Insets(m.top, m.left + w, m.bottom, m.right));
#spanend

JLabel label = new JLabel(image);
#spandel
label.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
#spanend
#spanadd
label.setCursor(Cursor.getDefaultCursor());
#spanend
label.setBorder(null);
label.setBounds(m.left, m.top, w, h);

field.add(label);

解説

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

解説

サンプルではsetMarginJTextFieldの左に余白を作り、そこにJLabelを配置することでアイコン(画像)を表示しています。 JComboBoxにアイコンを追加のように、Borderを使っても同様のことができますが、JTextComponentを継承したコンポーネントでは、setMarginを使用するとカーソルの指定などが簡単にできるのでおすすめです。
  • JComboBoxにアイコンを追加のようにBorderを使用する方法もあるが、JTextComponentを継承するコンポーネントではsetMargin(...)メソッドを使用するとカーソルの指定などが簡単に実現可能
    • JLabelの代わりにJButtonなどのコンポーネントを配置することも可能
  • JComboBoxEditorを取得してMarginを指定しても反映されない
  • JTextComponent#setMargin(...) (Java Platform SE 8)

    ただし、デフォルト以外の境界が設定されている場合は、Borderオブジェクトが適切なマージン空白を作成します(それ以外の場合、このプロパティーは事実上無視される)。

また、JLabelの代わりに、JButtonなどを置くこともできます(参考リンクを参照)。
JComboBoxのEditorを取得してMarginを指定しても、なぜかうまくいきません。
  • JTextFieldの右端にJLabelを置く場合は、以下のようにSpringLayoutを使用する方法もある
  • - JTextFieldの右端にJLabelを置く場合は、以下のようにSpringLayoutを使用する方法があります。
  • JButtonなどの高さを変更せずに幅を指定
    #spanend
    final JLabel label2 = new JLabel(image);
    #spandel
    label2.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    #spanend
    #spanadd
    label2.setCursor(Cursor.getDefaultCursor());
    #spanend
    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();
    #spandel
    field2.setMargin(new Insets(m.top+2,m.left,m.bottom,m.right+w));
    #spanend
    #spanadd
    field2.setMargin(new Insets(m.top + 2, m.left, m.bottom, m.right + w));
    #spanend
    

参考リンク

参考リンク

コメント

コメント