Swing/SearchBarLayoutComboBox のバックアップ差分(No.9)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/SearchBarLayoutComboBox へ行く。
- 1 (2010-09-20 (月) 12:16:36)
- 2 (2010-09-24 (金) 20:20:42)
- 3 (2010-11-16 (火) 02:38:36)
- 4 (2011-01-07 (金) 16:29:00)
- 5 (2011-05-25 (水) 17:46:55)
- 6 (2011-06-05 (日) 02:43:11)
- 7 (2012-12-27 (木) 07:44:52)
- 8 (2012-12-28 (金) 13:03:41)
- 9 (2013-05-26 (日) 05:18:05)
- 10 (2013-07-26 (金) 01:44:52)
- 11 (2014-02-18 (火) 15:26:20)
- 12 (2014-12-02 (火) 01:51:27)
- 13 (2015-02-22 (日) 21:04:03)
- 14 (2015-03-28 (土) 15:37:50)
- 15 (2015-05-22 (金) 18:59:56)
- 16 (2017-03-14 (火) 14:34:56)
- 17 (2018-01-11 (木) 17:46:50)
- 18 (2018-02-24 (土) 19:51:30)
- 19 (2019-05-22 (水) 19:34:28)
- 20 (2020-01-08 (水) 20:43:16)
- 21 (2021-07-10 (土) 04:34:22)
- 22 (2021-11-17 (水) 04:17:12)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JComboBox内にJButtonを左右に二つレイアウトする #navi(../) #tags(JComboBox, JButton, ArrowButton, LayoutManager, JTextField, PopupMenuListener) RIGHT:Posted by &author(aterai); at 2010-09-20 *JComboBox内にJButtonを左右に二つレイアウトする [#a4ce349d] ``JComboBox``が使用するレイアウトを変更して、検索欄風のコンポーネントを作成します。 -&jnlp; -&jar; -&zip; //#screenshot #ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTSqLxrLoI/AAAAAAAAAjI/M2OZzogy3-Q/s800/SearchBarLayoutComboBox.png) **サンプルコード [#k8b9a8a7] #code(link){{ public class BasicSearchBarComboBoxUI extends SearchBarComboBoxUI{ public static javax.swing.plaf.ComponentUI createUI(JComponent c) { return new BasicSearchBarComboBoxUI(); } protected boolean isEditable = true; @Override protected void installDefaults() { super.installDefaults(); //comboBox.setEditable(true); comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); //comboBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); } @Override protected LayoutManager createLayoutManager() { return new LayoutManager() { @Override public void addLayoutComponent(String name, Component comp) {} @Override public void removeLayoutComponent(Component comp) {} @Override public Dimension preferredLayoutSize(Container parent) { return parent.getPreferredSize(); } @Override public Dimension minimumLayoutSize(Container parent) { return parent.getMinimumSize(); } @Override public void layoutContainer(Container parent) { if(!(parent instanceof JComboBox)) return; JComboBox cb = (JComboBox)parent; int width = cb.getWidth(); int height = cb.getHeight(); Insets insets = cb.getInsets(); int buttonHeight = height - (insets.top + insets.bottom); int buttonWidth = buttonHeight; int loupeWidth = buttonHeight; JButton arrowButton = (JButton)cb.getComponent(0); if(arrowButton != null) { Insets arrowInsets = arrowButton.getInsets(); buttonWidth = arrowButton.getPreferredSize().width + arrowInsets.left + arrowInsets.right; arrowButton.setBounds(insets.left, insets.top, buttonWidth, buttonHeight); } JButton loupeButton = null; for(Component c: cb.getComponents()) { if("ComboBox.loupeButton".equals(c.getName())) { loupeButton = (JButton)c; break; } } //= (JButton)cb.getComponent(3); if(loupeButton != null) { Insets loupeInsets = loupeButton.getInsets(); loupeWidth = loupeButton.getPreferredSize().width + loupeInsets.left + loupeInsets.right; loupeButton.setBounds(width - (insets.right + loupeWidth), insets.top, loupeWidth, buttonHeight); } JTextField editor = (JTextField)cb.getEditor().getEditorComponent(); //JTextField editor = (JTextField)cb.getComponent(1); if(editor != null) { editor.setBounds(insets.left + buttonWidth, insets.top, width - (insets.left + insets.right + buttonWidth + loupeWidth), height - (insets.top + insets.bottom)); } } }; } //...... }} **解説 [#g9ab284c] 上記のサンプルでは、``JComboBox``が使用するレイアウトを以下のように変更しています。 -元の``ArrowButton``は、左側に表示 --``JComboBox#setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);``とした場合のコードを流用 --アイコンも検索エンジンのものと、下向きの三角の二つを表示するように設定 -``LoupeButton``として、新たに``JButton``を追加し、右側に配置 -常に編集可能として、``JTextField``を中央に配置 ---- ポップアップを表示、選択しても``JTextField``が変更されないようにしています。 -``JComboBox#putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);``として、カーソル移動で変更されないように設定 -選択されても``PopupMenuListener``で``setText``し直すように設定 - ``JComboBox#putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);``として、カーソル移動で変更されないように設定 - 選択されても``PopupMenuListener``で``setText``し直すように設定 #code{{ protected PopupMenuListener createPopupMenuListener() { if(popupMenuListener == null) { popupMenuListener = new PopupMenuListener() { private String str; @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) { JComboBox combo = (JComboBox)e.getSource(); str = combo.getEditor().getItem().toString(); } @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { Object o = listBox.getSelectedValue(); if(o!=null && o instanceof SearchEngine) { SearchEngine se = (SearchEngine) o; arrowButton.setIcon(se.favicon); } final JComboBox combo = (JComboBox)e.getSource(); EventQueue.invokeLater(new Runnable() { @Override public void run() { combo.getEditor().setItem(str); } }); } @Override public void popupMenuCanceled(PopupMenuEvent e) {} }; } return popupMenuListener; } }} //**参考リンク **コメント [#ca736f58] - ``editor``にフォーカスがある場合、左のボタンをクリックしてもポップアップメニューが開かないバグを修正。 -- [[aterai]] &new{2010-09-24 (金) 20:20:42}; - ``ArrawButton``をクリックして、カーソルキーを押すと``NullPointerException``が発生するバグを修正(この修正?で、ポップアップが表示されている状態で、例えば``g``キーを押しても、項目の``google``は選択されなくなった)。 -- [[aterai]] &new{2011-01-07 (金) 16:29:00}; - ``LookAndFeel``を切り替えると、``JComboBox``のフォント設定で、``NullPointerException``が発生するのを修正。 -- [[aterai]] &new{2011-05-25 (水) 17:46:55}; #comment