---
category: swing
folder: AuxiliaryLookAndFeel
title: AuxiliaryLookAndFeelを追加する
tags: [LookAndFeel, AuxiliaryLookAndFeel, UIManager, JComboBox]
author: aterai
pubdate: 2012-04-09T14:26:00+09:00
description: AuxiliaryLookAndFeelを追加して、WindowsLookAndFeelの場合の動作を変更します。
image: https://lh4.googleusercontent.com/-SxIyCqWRFhk/T4JxXw96NSI/AAAAAAAABLQ/gM_5mjZPn1o/s800/AuxiliaryLookAndFeel.png
---
* 概要 [#summary]
`AuxiliaryLookAndFeel`を追加して、`WindowsLookAndFeel`の場合の動作を変更します。

#download(https://lh4.googleusercontent.com/-SxIyCqWRFhk/T4JxXw96NSI/AAAAAAAABLQ/gM_5mjZPn1o/s800/AuxiliaryLookAndFeel.png)

* サンプルコード [#sourcecode]
#code(link){{
JCheckBox check = (JCheckBox) e.getSource();
String lnf = UIManager.getLookAndFeel().getName();
if (check.isSelected() && lnf.contains("Windows")) {
  UIManager.addAuxiliaryLookAndFeel(auxLookAndFeel);
} else {
  UIManager.removeAuxiliaryLookAndFeel(auxLookAndFeel);
}
SwingUtilities.updateComponentTreeUI(getRootPane());
}}

* 解説 [#explanation]
`WindowsLookAndFeel`の場合、それを修正することなく、`JComboBox`のドロップダウンリストで右クリックを無効にするような`ComboBoxUI`を`UIManager.addAuxiliaryLookAndFeel(...)`を使って追加しています。

- [[JComboBoxのドロップダウンリストで右クリックを無効化>Swing/DisableRightClick]]

#code{{
public class AuxiliaryWindowsComboBoxUI extends WindowsComboBoxUI {
  public static ComponentUI createUI(JComponent c) {
    return new AuxiliaryWindowsComboBoxUI();
  }

  @Override protected ComboPopup createPopup() {
    return new BasicComboPopup2(comboBox);
  }

  @Override public void addEditor() {
    removeEditor();
    ComboBoxEditor cbe = comboBox.getEditor();
    if (cbe != null) {
      editor = cbe.getEditorComponent();
      if (editor != null) {
        configureEditor();
        comboBox.add(editor);
        if (comboBox.isFocusOwner()) {
          editor.requestFocusInWindow();
        }
      }
    }
  }

  //Override all UI-specific methods your UI classes inherit.
  @Override public void removeEditor() {}

  @Override protected void configureEditor() {}

  @Override protected void unconfigureEditor() {}

  @Override public void update(Graphics g, JComponent c) {}

  @Override public void paint(Graphics g, JComponent c) {}
  // ...
}}

- 注:
-- `LookAndFeel`を`Nimbus`にすると`ClassCastException`が発生する
--- [https://bugs.openjdk.java.net/browse/JDK-6631956 [JDK-6631956] Nimbus: ClassCastException when running with MultiLookAndFeel - Java Bug System]
-- `UIManager.addPropertyChangeListener(new PropertyChangeListener() {...});`を追加して、`WindowsLookAndFeel`以外の場合は、`UIManager.removeAuxiliaryLookAndFeel(auxLookAndFeel);`
-- 編集可能な`JComboBox`の場合、`NullPointerException`が発生する
--- `WindowsComboBoxUI#addEditor()`をオーバーライド
- `LookAndFeel`を`Nimbus`にすると`ClassCastException`が発生する
-- [https://bugs.openjdk.java.net/browse/JDK-6631956 [JDK-6631956] Nimbus: ClassCastException when running with MultiLookAndFeel - Java Bug System]
- `UIManager.addPropertyChangeListener(new PropertyChangeListener() {...});`を追加して、`WindowsLookAndFeel`以外の場合は、`UIManager.removeAuxiliaryLookAndFeel(auxLookAndFeel);`
- 編集可能な`JComboBox`の場合、`NullPointerException`が発生する
-- `WindowsComboBoxUI#addEditor()`をオーバーライド

* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/plaf/multi/doc-files/multi_tsc.html 多重Look & Feelの使用]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/UIManager.html#addAuxiliaryLookAndFeel-javax.swing.LookAndFeel- UIManager#addAuxiliaryLookAndFeel(LookAndFeel) (Java Platform SE 8)]

* コメント [#comment]
#comment
- `AuxiliaryLookAndFeel`の作成方法、使い方などを間違えているような気がするが…。 -- &user(aterai); &new{2012-04-09 (月) 14:41:52};

#comment