Swing/ComboBoxSeparator のバックアップ(No.4)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/ComboBoxSeparator へ行く。
- 1 (2005-08-27 (土) 16:01:47)
- 2 (2005-08-29 (月) 02:54:20)
- 3 (2005-08-29 (月) 02:58:20)
- 4 (2005-08-29 (月) 08:12:35)
- 5 (2005-09-20 (火) 09:16:03)
- 6 (2006-02-27 (月) 15:35:14)
- 7 (2006-04-12 (水) 19:38:37)
- 8 (2007-03-02 (金) 16:24:42)
- 9 (2007-08-10 (金) 18:51:42)
- 10 (2008-04-11 (金) 15:40:27)
- 11 (2013-03-27 (水) 16:34:01)
- 12 (2013-04-10 (水) 16:18:44)
- 13 (2013-07-27 (土) 00:59:51)
- 14 (2015-01-09 (金) 14:13:46)
- 15 (2016-04-22 (金) 15:53:46)
- 16 (2016-09-22 (木) 21:21:34)
- 17 (2017-11-04 (土) 00:06:32)
- 18 (2019-06-07 (金) 15:26:28)
- 19 (2021-02-21 (日) 03:52:00)
Terai Atsuhiro 2021-02-21 (日) 03:52:16
final ListCellRenderer lcr = combobox.getRenderer(); combobox.setRenderer(new ListCellRenderer() { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if(value instanceof JSeparator) { return (JSeparator)value; }else{ return (JLabel)lcr.getListCellRendererComponent( list,value,index,isSelected,cellHasFocus); } } }); DefaultComboBoxModel model = new DefaultComboBoxModel() { public void setSelectedItem(Object anObject) { if(!(anObject instanceof JSeparator)) { super.setSelectedItem(anObject); } } }; model.addElement("aaaa"); model.addElement(new JSeparator()); model.addElement("bbb1"); combobox.setModel(model);
- ListCellRenderer
- DefaultComboBoxModel
combobox.addItemListener(new ItemListener() { private Object prev; public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED) { Object obj = e.getItem(); if(obj instanceof JSeparator) { if(prev==null) prev = combobox.getItemAt(0); combobox.setSelectedItem(prev); }else{ prev = obj; } } } });
public class MainPanel extends JPanel{ private final JComboBox combobox = new JComboBox(); public MainPanel() { 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); setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(new JTextField()); add(Box.createVerticalStrut(5)); add(combobox); add(Box.createRigidArea(new Dimension(320,5))); setBorder(BorderFactory.createEmptyBorder(5,5,0,5)); } 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; } }