Swing/DisableItemComboBox のバックアップ差分(No.4)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/DisableItemComboBox へ行く。
- 1 (2008-04-14 (月) 13:34:30)
- 2 (2008-04-14 (月) 19:30:02)
- 3 (2011-05-04 (水) 18:59:13)
- 4 (2014-03-18 (火) 18:51:50)
- 5 (2014-03-19 (水) 12:37:50)
- 6 (2014-12-02 (火) 01:46:30)
- 7 (2016-02-19 (金) 15:07:02)
- 8 (2017-07-13 (木) 13:25:07)
- 9 (2018-02-24 (土) 19:51:30)
- 10 (2018-07-12 (木) 18:09:29)
- 11 (2020-07-10 (金) 16:58:24)
- 12 (2021-12-10 (金) 15:37:27)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JComboBoxのアイテムを選択不可にする #navi(../) RIGHT:Posted by [[aterai]] at 2008-04-14 *JComboBoxのアイテムを選択不可にする [#q640bc3b] JComboBoxのドロップダウンリストで、指定したアイテムを選択不可にします。 #tags(JComboBox, ListCellRenderer, ActionMap, InputMap) RIGHT:Posted by &author(aterai); at 2008-04-14 * JComboBoxのアイテムを選択不可にする [#q640bc3b] `JComboBox`のドロップダウンリストで、指定したアイテムを選択不可にします。 -&jnlp; -&jar; -&zip; #download #ref(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTLHzjDYpI/AAAAAAAAAXE/M4bkzWJetUI/s800/DisableItemComboBox.png) //#screenshot #ref(http://lh3.ggpht.com/_9Z4BYR88imo/TQTLHzjDYpI/AAAAAAAAAXE/M4bkzWJetUI/s800/DisableItemComboBox.png) **サンプルコード [#eb0a9dc4] #code{{ ** サンプルコード [#eb0a9dc4] #code(link){{ class MyComboBox extends JComboBox { public MyComboBox() { super(); final ListCellRenderer r = getRenderer(); setRenderer(new ListCellRenderer() { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Component c; if(disableIndexSet.contains(index)) { c = r.getListCellRendererComponent(list,value,index,false,false); c.setEnabled(false); }else{ c = r.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus); c.setEnabled(true); } return c; } }); Action up = new AbstractAction() { public void actionPerformed(ActionEvent e) { @Override public void actionPerformed(ActionEvent e) { int si = getSelectedIndex(); for(int i = si-1;i>=0;i--) { if(!disableIndexSet.contains(i)) { setSelectedIndex(i); break; } } } }; Action down = new AbstractAction() { public void actionPerformed(ActionEvent e) { @Override public void actionPerformed(ActionEvent e) { int si = getSelectedIndex(); for(int i = si+1;i<getModel().getSize();i++) { if(!disableIndexSet.contains(i)) { setSelectedIndex(i); break; } } } }; ActionMap am = getActionMap(); am.put("selectPrevious3", up); am.put("selectNext3", down); InputMap im = getInputMap(); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "selectPrevious3"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_KP_UP, 0), "selectPrevious3"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "selectNext3"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_KP_DOWN, 0), "selectNext3"); } private final HashSet<Integer> disableIndexSet = new HashSet<Integer>(); private boolean isDisableIndex = false; public void setDisableIndex(HashSet<Integer> set) { disableIndexSet.clear(); for(Integer i:set) { disableIndexSet.add(i); } } @Override public void setPopupVisible(boolean v) { @Override public void setPopupVisible(boolean v) { if(!v && isDisableIndex) { isDisableIndex = false; }else{ super.setPopupVisible(v); } } @Override public void setSelectedIndex(int index) { @Override public void setSelectedIndex(int index) { if(disableIndexSet.contains(index)) { isDisableIndex = true; }else{ //isDisableIndex = false; super.setSelectedIndex(index); } } } }} **解説 [#qdc48e21] ** 解説 [#qdc48e21] 上記のサンプルでは、以下の方法でドロップダウンリストの特定のアイテムを選択できないように設定しています。 - 表示 -- セルレンダラーでsetEnabledなどを設定 -- セルレンダラーで`setEnabled`などを設定 - 選択不可 -- setSelectedIndexをオーバーライド -- `setSelectedIndex`をオーバーライド - 選択不可アイテムをクリックしてもポップアップを閉じない -- setPopupVisibleをオーバーライド -- `setPopupVisible`をオーバーライド - キー操作で選択不可アイテムを無視 -- ActionMap、InputMapの設定 -- `ActionMap`、`InputMap`の設定 **参考リンク [#ic0f4012] -[[JListの任意のItemを選択不可にする>Swing/DisabledItem]] ** 参考リンク [#ic0f4012] - [[JListの任意のItemを選択不可にする>Swing/DisabledItem]] **コメント [#v2920269] ** コメント [#v2920269] #comment