Swing/HideComboArrowButton のバックアップの現在との差分(No.2)
TITLE:JComboBoxのArrowButtonを隠す
Posted by aterai at 2008-12-22
JComboBoxのArrowButtonを隠す
ArrowButtonを隠して、JComboBoxの表示をJLabel風にします。- 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
風にします。
- &jnlp;
- &jar;
- &zip;
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() {
protected JButton createArrowButton() {
@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風に表示しています。- UIManager.put("ComboBox.squareButton", Boolean.FALSE); で、ArrowButtonの幅をそのまま使用するように変更
- BasicComboBoxUI#createArrowButton をオーバーライドして、ArrowButtonの代わりに幅高さ0でsetVisible(false)なボタンを作成
- JComboBoxの背景色を親のJPanelと同じにする
- JComboBoxがフォーカスを取得しないようにする
解説
上記のサンプルでは、以下のようにしてJComboBox
の矢印ボタンを非表示に設定しています。
参考リンク
- Swing - Hide JComboBox Arrow?
-
UIManager.put("ComboBox.squareButton", Boolean.FALSE)
を設定してJComboBox
の高さと同じ幅ではなくArrowButton
の幅をそのまま使用するように変更 -
BasicComboBoxUI#createArrowButton
をオーバーライドしてArrowButton
の代わりに幅と高さが0
でsetVisible(false)
なJButton
を作成 -
JComboBox
の背景色を親のJPanel
と同じ色に変更 -
JComboBox
がフォーカスを取得不可になるよう設定