• 追加された行はこの色です。
  • 削除された行はこの色です。
---
title: AuxiliaryLookAndFeelを追加する
tags: [LookAndFeel, UIManager, JComboBox]
author: aterai
pubdate: 2012-04-09T14:26:00+09:00
description: AuxiliaryLookAndFeelを追加して、WindowsLookAndFeelの場合の動作を変更します。
---
* 概要 [#ua0f3e60]
`AuxiliaryLookAndFeel`を追加して、`WindowsLookAndFeel`の場合の動作を変更します。

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

* サンプルコード [#lc13bf97]
#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(MainPanel.this);
SwingUtilities.updateComponentTreeUI(getRootPane());
}}

* 解説 [#r33184aa]
`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`が発生する
--- [http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6631956 Bug ID: 6631956 Nimbus: ClassCastException when running with MultiLookAndFeel]
-- `UIManager.addPropertyChangeListener(new PropertyChangeListener() {...});`を追加して、`WindowsLookAndFeel`以外の場合は、`UIManager.removeAuxiliaryLookAndFeel(auxLookAndFeel);`
-- 編集可能な`JComboBox`の場合、`NullPointerException`が発生する
--- `WindowsComboBoxUI#addEditor()`をオーバーライド

* 参考リンク [#ybc6bf3b]
- [http://docs.oracle.com/javase/7/docs/api/javax/swing/plaf/multi/doc-files/multi_tsc.html Using the Multiplexing Look and Feel]

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

#comment