TITLE:JTableのCellEditorに設定したJComboBoxに余白を追加する
#navi(../)
RIGHT:Posted by [[aterai]] at 2012-05-07
*JTableのCellEditorに設定したJComboBoxに余白を追加する [#kc1c586e]
JTableのCellEditorに設定したJComboBoxに余白を追加します。

-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(https://lh4.googleusercontent.com/-dIea13PoJ70/T6c7YezP1BI/AAAAAAAABMQ/e0IqDjxhjpw/s800/ComboBoxCellEditorInsets.png)

**サンプルコード [#z0adf2e6]
#code(link){{
class ComboBoxPanel extends JPanel {
  public final JComboBox comboBox = new JComboBox(new String[] {"aaaaaa", "bbb", "c"});
  public ComboBoxPanel() {
    super(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.weightx = 1.0;
    c.insets = new Insets(0, 10, 0, 10);
    c.fill = GridBagConstraints.HORIZONTAL;

    comboBox.setEditable(true);
    setOpaque(true);
    add(comboBox, c);
    comboBox.setSelectedIndex(0);
  }
}
}}

**解説 [#w805be00]
- Border(左)
-- JComboBox自身に余白を設定し、これをCellRenderer, CellEditorに使用
-- ドロップダウンリストの位置、サイズが余白を含んだ幅になる

#code{{
combo.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(8,10,8,10), combo.getBorder()));
}}

- JPanel+JComboBox(右)
-- GridBagLayout を使用するJPanelにJComboBox を追加
-- fill フィールドを GridBagConstraints.HORIZONTAL として、垂直にはJComboBox のサイズを変更しない
-- insets フィールドを設定して、JComboBoxの外側に別途(ドロップダウンリストの位置、サイズに影響しないように)余白を追加

**参考リンク [#j76de487]
-[[JTableのCellRendererにJComboBoxを設定>Swing/ComboCellRenderer]]

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