TITLE:AuxiliaryLookAndFeelを追加する
#navi(../)
RIGHT:Posted by [[aterai]] at 2012-04-09
*AuxiliaryLookAndFeelを追加する [#ua0f3e60]
AuxiliaryLookAndFeelを追加して、WindowsLookAndFeelの場合の動作を変更します。

-&jnlp;
-&jar;
-&zip;

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

**サンプルコード [#lc13bf97]
#code{{
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);
}}

**解説 [#r33184aa]
WindowsLookAndFeelの場合、JComboBoxのドロップダウンリストで右クリックを無効にするようなBomboBoxUIを追加しています。

#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.sun.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