Swing/BorderSeparator のバックアップ差分(No.9)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/BorderSeparator へ行く。
- 1 (2006-02-27 (月) 15:28:49)
- 2 (2006-04-12 (水) 19:34:47)
- 3 (2006-11-10 (金) 13:51:31)
- 4 (2007-04-09 (月) 18:01:27)
- 5 (2007-05-27 (日) 01:31:08)
- 6 (2007-08-13 (月) 14:44:44)
- 7 (2008-06-19 (木) 14:29:25)
- 8 (2008-09-04 (木) 17:43:39)
- 9 (2008-11-23 (日) 21:41:10)
- 10 (2010-12-13 (月) 00:05:32)
- 11 (2013-02-20 (水) 15:28:56)
- 12 (2013-03-24 (日) 21:28:07)
- 13 (2013-10-18 (金) 15:53:08)
- 14 (2015-01-14 (水) 15:44:15)
- 15 (2015-03-12 (木) 15:09:25)
- 16 (2015-03-19 (木) 16:30:59)
- 17 (2015-03-25 (水) 17:01:05)
- 18 (2016-09-07 (水) 15:37:42)
- 19 (2017-10-17 (火) 15:19:21)
- 20 (2018-09-27 (木) 20:51:20)
- 21 (2019-05-22 (水) 19:34:28)
- 22 (2020-09-26 (土) 17:34:34)
- 23 (2022-05-31 (火) 17:36:21)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JComboBoxのアイテムをBorderで修飾してグループ分け #navi(../) *JComboBoxのアイテムをBorderで修飾してグループ分け [#zb21cb91] Posted by [[terai]] at 2005-09-20 #contents **概要 [#n40caa06] JComboBoxのアイテムをBorderを使用して修飾してグループ分けします。[[プログラム板 Java低速GUI Swing 3>http://pc8.2ch.net/test/read.cgi/tech/1121700954/]]の229さんのアイデアを参考にしています。 -&jnlp; -&jar; -&zip; #screenshot **サンプルコード [#x381b337] #code{{ 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]]する方法より簡単に区切りを表現することができます。 ---- コンボボックスが編集不可の場合は、[[JComboBox Items with Separators - Santhosh Kumar's Weblog>http://www.jroller.com/santhosh/entry/jcombobox_items_with_separators]]のようにフィールド表示(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] -[[プログラム板 Java低速GUI Swing 3>http://pc8.2ch.net/test/read.cgi/tech/1121700954/]] -[[JComboBoxにJSeparatorを挿入>Swing/ComboBoxSeparator]] -[[JComboBox Items with Separators - Santhosh Kumar's Weblog>http://www.jroller.com/santhosh/entry/jcombobox_items_with_separators]] **コメント [#f99d5b78] - index!=-1を追加、スクリーンショットを更新 -- [[terai]] &new{2008-09-04 (木) 17:53:47}; #comment