Swing/BorderSeparator のバックアップの現在との差分(No.8)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - 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 --- category: swing folder: BorderSeparator title: JComboBoxのアイテムをBorderで修飾してグループ分け tags: [JComboBox, Border, ListCellRenderer, MatteBorder] author: aterai pubdate: 2005-09-19T09:10:06+09:00 description: JComboBoxのアイテムをBorderを使用して修飾してグループ分けします。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTIMVjWegI/AAAAAAAAASY/yM_W_tfnios/s800/BorderSeparator.png --- * 概要 [#summary] `JComboBox`のアイテムを`Border`を使用して修飾してグループ分けします。 #contents #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTIMVjWegI/AAAAAAAAASY/yM_W_tfnios/s800/BorderSeparator.png) **概要 [#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(); * サンプルコード [#sourcecode] #code(link){{ JComboBox combobox = new JComboBox(); JSeparator sep = new JSeparator(); 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()) { @Override 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{ 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{ class MyItem { private final String item; private final boolean flag; public MyItem(String str) { this(str,false); this(str, false); } public MyItem(String str, boolean flg) { item = str; flag = flg; } public String toString() { @Override public String toString() { return item; } public boolean hasSeparator() { return flag; } } //...... // ... }} **解説 [#uaa4e2ae] レンダラーの中で、JLabelをMatteBorderで修飾し、JSeparatorを使用せずにItemをグループ分けしているように見せかけています。 * 解説 [#explanation] レンダラーの中で`JLabel`を`MatteBorder`で修飾し、`JSeparator`を使用せずに`Item`をグループ分けしているように見せかけています。 コンボボックスが編集可の場合は、フィールド表示にはレンダラーではなく、JTextFieldが使用されるため、[[JComboBoxにJSeparatorを挿入>Swing/ComboBoxSeparator]]する方法より簡単に区切りを表現することができます。 - `JComboBox`が編集可 -- フィールド表示には`ListCellRenderer`ではなく`JTextField`が使用されるため、[[JComboBoxにJSeparatorを挿入>Swing/ComboBoxSeparator]]する方法より簡単に区切りを表現可能 - `JComboBox`が編集不可 -- [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() { @Override 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]] * 参考リンク [#reference] - [[JComboBoxにJSeparatorを挿入>Swing/ComboBoxSeparator]] - [http://www.jroller.com/santhosh/entry/jcombobox_items_with_separators JComboBox Items with Separators - Santhosh Kumar's Weblog] **コメント [#f99d5b78] * コメント [#comment] #comment - `index!=-1`を追加、スクリーンショットを更新 -- &user(aterai); &new{2008-09-04 (木) 17:53:47}; #comment