Swing/HideComboArrowButton のバックアップの現在との差分(No.8)
- category: swing folder: HideComboArrowButton title: JComboBoxのArrowButtonを隠す tags: [JComboBox, ArrowButton, UIManager] author: aterai pubdate: 2008-12-22T13:06:25+09:00 description: ArrowButtonを隠して、JComboBoxの表示をJLabel風にします。 image:
概要
概要
ArrowButton
を隠して、JComboBox
の表示をJLabel
風にします。
Screenshot
Advertisement
サンプルコード
サンプルコード
#spandel
JPanel p = new JPanel(new BorderLayout(5, 5));
#spanend
Object[] items = {"JComboBox 11111:", "JComboBox 222:", "JComboBox 33:"};
#spandel
#spanend
UIManager.put("ComboBox.squareButton", Boolean.FALSE);
JComboBox comboBox = new JComboBox(items);
comboBox.setUI(new BasicComboBoxUI() {
@Override protected JButton createArrowButton() {
JButton button = new JButton(); //super.createArrowButton();
button.setBorder(BorderFactory.createEmptyBorder());
button.setVisible(false);
return button;
}
});
comboBox.setOpaque(true);
comboBox.setBackground(p.getBackground());
#spandel
comboBox.setBorder(BorderFactory.createEmptyBorder(0,2,0,2));
#spanend
#spanadd
comboBox.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
#spanend
comboBox.setFocusable(false);
UIManager.put("ComboBox.squareButton", Boolean.TRUE);
View in GitHub: Java, Kotlin解説
上記のサンプルでは、以下のようにして、JComboBox
をJLabel
風に表示しています。
解説
上記のサンプルでは、以下のようにしてJComboBox
の矢印ボタンを非表示に設定しています。
-
UIManager.put("ComboBox.squareButton", Boolean.FALSE);
で、ArrowButton
の幅をそのまま使用するように変更 -
BasicComboBoxUI#createArrowButton
をオーバーライドして、ArrowButton
の代わりに幅高さ0
でsetVisible(false)
なボタンを作成 -
JComboBox
の背景色を親のJPanel
と同じにする -
JComboBox
がフォーカスを取得しないようにする -
UIManager.put("ComboBox.squareButton", Boolean.FALSE)
を設定してJComboBox
の高さと同じ幅ではなくArrowButton
の幅をそのまま使用するように変更 -
BasicComboBoxUI#createArrowButton
をオーバーライドしてArrowButton
の代わりに幅と高さが0
でsetVisible(false)
なJButton
を作成 -
JComboBox
の背景色を親のJPanel
と同じ色に変更 -
JComboBox
がフォーカスを取得不可になるよう設定
参考リンク
参考リンク
- Swing - Hide JComboBox Arrow?
- Bug ID: 6337518 Null Arrow Button Throws Exception in BasicComboBoxUI
- Bug ID: 6337518 Null Arrow Button Throws Exception in BasicComboBoxUI