JComboBoxのArrowButtonを隠す
Total: 10280, Today: 2, Yesterday: 1
Posted by aterai at
Last-modified:
Summary
ArrowButtonを隠して、JComboBoxの表示をJLabel風にします。
Screenshot

Advertisement
Source Code Examples
Object[] items = {"JComboBox 11111:", "JComboBox 222:", "JComboBox 33:"};
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());
comboBox.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
comboBox.setFocusable(false);
UIManager.put("ComboBox.squareButton", Boolean.TRUE);
View in GitHub: Java, KotlinDescription
上記のサンプルでは、以下のようにしてJComboBoxの矢印ボタンを非表示に設定しています。
UIManager.put("ComboBox.squareButton", Boolean.FALSE)を設定してJComboBoxの高さと同じ幅ではなくArrowButtonの幅をそのまま使用するように変更BasicComboBoxUI#createArrowButtonをオーバーライドしてArrowButtonの代わりに幅と高さが0でsetVisible(false)なJButtonを作成JComboBoxの背景色を親のJPanelと同じ色に変更JComboBoxがフォーカスを取得不可になるよう設定