TITLE:JComboBoxのアイテムをBorderで修飾してグループ分け
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2005-09-19
*JComboBoxのアイテムをBorderで修飾してグループ分け [#zb21cb91]
JComboBoxのアイテムをBorderを使用して修飾してグループ分けします。

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

//#screenshot
#ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTIMVjWegI/AAAAAAAAASY/yM_W_tfnios/s800/BorderSeparator.png)

**サンプルコード [#x381b337]
#code(link){{
final JComboBox combobox = new JComboBox();
final JSeparator sep = new JSeparator();
final ListCellRenderer lcr = combobox.getRenderer();
combobox.setRenderer(new ListCellRenderer() {
  public Component getListCellRendererComponent(
               JList list, Object value, int index,
               boolean isSelected, boolean cellHasFocus) {
    MyItem item = (MyItem)value;
    JLabel label = (JLabel)lcr.getListCellRendererComponent(
                    list,item,index,isSelected,cellHasFocus);
    if(item.hasSeparator()) {
      label.setBorder(
             BorderFactory.createMatteBorder(1,0,0,0,Color.GRAY));
    }else{
      label.setBorder(BorderFactory.createEmptyBorder());
    }
    return label;
  }
});
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(new MyItem("aaaa"));
model.addElement(new MyItem("eeeeeeeee", true));
model.addElement(new MyItem("bbb12"));
combobox.setModel(model);
combobox.setEditable(true);
}}

#code{{
class MyItem{
  private final String  item;
  private final boolean flag;
  public MyItem(String str) {
    this(str,false);
  }
  public MyItem(String str, boolean flg) {
    item = str;
    flag = flg;
  }
  public String toString() {
    return item;
  }
  public boolean hasSeparator() {
    return flag;
  }
}
//......
}}

**解説 [#uaa4e2ae]
レンダラーの中で、JLabelをMatteBorderで修飾し、JSeparatorを使用せずにItemをグループ分けしているように見せかけています。

コンボボックスが編集可の場合は、フィールド表示にはレンダラーではなく、JTextFieldが使用されるため、[[JComboBoxにJSeparatorを挿入>Swing/ComboBoxSeparator]]する方法より簡単に区切りを表現することができます。

----
コンボボックスが編集不可の場合は、[http://www.jroller.com/santhosh/entry/jcombobox_items_with_separators JComboBox Items with Separators - Santhosh Kumar's Weblog]のようにフィールド表示(index!=-1の場合)で区切りが表示されないようにする必要があります。
#code{{
combobox.setRenderer(new ListCellRenderer() {
  public Component getListCellRendererComponent(
               JList list, Object value, int index,
               boolean isSelected, boolean cellHasFocus) {
    MyItem item = (MyItem)value;
    JLabel label = (JLabel)lcr.getListCellRendererComponent(
                    list,item,index,isSelected,cellHasFocus);
    if(index!=-1 && item.hasSeparator()) {
      label.setBorder(
             BorderFactory.createMatteBorder(1,0,0,0,Color.GRAY));
    }else{
      label.setBorder(BorderFactory.createEmptyBorder());
    }
    return label;
  }
});

}}

**参考リンク [#w4c1b8bb]
-[[JComboBoxにJSeparatorを挿入>Swing/ComboBoxSeparator]]
-[http://www.jroller.com/santhosh/entry/jcombobox_items_with_separators JComboBox Items with Separators - Santhosh Kumar's Weblog]

**コメント [#f99d5b78]
- index!=-1を追加、スクリーンショットを更新 -- [[aterai]] &new{2008-09-04 (木) 17:53:47};

#comment