---
category: swing
folder: ComboBoxAsButtonWhenNotEditable
title: NimbusLookAndFeelで編集不可のJComboBoxがボタンのようにロールオーバー表示するかを切り替える
tags: [JComboBox, NimbusLookAndFeel, UIManager, UIDefaults]
author: aterai
pubdate: 2022-04-04T00:39:59+09:00
description: NimbusLookAndFeelで編集不可のJComboBox上にマウスカーソルが乗るとJButtonのようにロールオーバー状態を描画するかを切り替えます。
image: https://drive.google.com/uc?id=1xVBgDBbUhT_vj3ZWTBj_2wsijPDBy-I6
---
* 概要 [#summary]
`NimbusLookAndFeel`で編集不可の`JComboBox`上にマウスカーソルが乗ると`JButton`のようにロールオーバー状態を描画するかを切り替えます。

#download(https://drive.google.com/uc?id=1xVBgDBbUhT_vj3ZWTBj_2wsijPDBy-I6)

* サンプルコード [#sourcecode]
#code(link){{
JComboBox<String> combo = new JComboBox<>(model);
UIDefaults d = UIManager.getLookAndFeelDefaults();
d.put("ComboBox.buttonWhenNotEditable", false);
combo.putClientProperty("Nimbus.Overrides", d);
combo.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
}}

* 解説 [#explanation]
- `UIManager.put("ComboBox.buttonWhenNotEditable", true)`
-- `NimbusLookAndFeel`のデフォルトで編集不可の`JComboBox`は`JComboBox`内部の`ArrowButton(JButton)`と合わせてマウスカーソルでのロールオーバーやプレス状態で描画が変化する
-- `WindowsLookAndFeel`では`ComboBox.buttonWhenNotEditable`の設定には依存せず常にロールオーバー状態で描画が変化する
-- 編集可能な`JComboBox`では`ComboBox.buttonWhenNotEditable`の設定は無効
- `UIManager.put("ComboBox.buttonWhenNotEditable", false)`
-- 上記のサンプルでは`JComboBox#putClientProperty(...)`で下の`JComboBox`のみ編集不可の`JComboBox`のロールオーバー描画を無効に設定している
-- `JComboBox`内部の`ArrowButton(JButton)`もロールオーバー描画が無効になる
-- `MetalLookAndFeel`では`ComboBox.buttonWhenNotEditable`の設定には依存せず常にロールオーバー状態で描画は変化しない

* 参考リンク [#reference]
- [[NimbusLookAndFeelを使用する>Swing/NimbusLookAndFeel]]

* コメント [#comment]
#comment
#comment