JComboBoxのドロップダウンリストに角丸のBorderを設定する
Total: 4214, Today: 1, Yesterday: 0
Posted by aterai at
Last-modified:
Summary
JComboBoxからBasicComboPopupを取得し、これに角丸のBorderを設定します。
Screenshot

Advertisement
Source Code Examples
JComboBox<String> combo1 = new JComboBox<String>(makeModel()) {
private transient PopupMenuListener listener;
@Override public void updateUI() {
removePopupMenuListener(listener);
UIManager.put("ComboBox.border", new RoundedCornerBorder());
super.updateUI();
setUI(new BasicComboBoxUI());
listener = new HeavyWeightContainerListener();
addPopupMenuListener(listener);
Object o = getAccessibleContext().getAccessibleChild(0);
if (o instanceof JComponent) {
JComponent c = (JComponent) o;
c.setBorder(new RoundedCornerBorder());
c.setForeground(FOREGROUND);
c.setBackground(BACKGROUND);
}
}
};
View in GitHub: Java, KotlinDescription
- 上:
UIManager.put(...)でJComboBoxの背景色などを変更してBasicComboBoxUIを設定
- 中:
- 上の
JComboBoxからgetAccessibleContext().getAccessibleChild(0)でBasicComboPopupを取得し角丸のBorderを設定 JComboBoxにPopupMenuListenerを追加しドロップダウンリストがJFrameの外側にはみ出す(HeavyWeightContainerのJWindowにJPopupMenuが配置されている)場合は、JWindowの背景を透明化して角丸部分を非表示に設定
- 上の
- 下:
- 中の
JComboBoxと同様にBasicComboPopupを取得して下辺のみ角丸のBorderを設定JComboBox自体には上辺のみ角丸のBorderを設定
ArrowButtonを変更
- 中の