Swing/AuxiliaryLookAndFeel のバックアップの現在との差分(No.7)
- 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:
概要
AuxiliaryLookAndFeel
を追加して、WindowsLookAndFeel
の場合の動作を変更します。
Screenshot
Advertisement
サンプルコード
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());
View in GitHub: Java, Kotlin解説
WindowsLookAndFeel
の場合、JComboBox
のドロップダウンリストで右クリックを無効にするようなComboBoxUI
をUIManager.addAuxiliaryLookAndFeel(...)
を使って追加しています。
WindowsLookAndFeel
の場合、それを修正することなくJComboBox
のドロップダウンリストで右クリックを無効にするようなComboBoxUI
をUIManager.addAuxiliaryLookAndFeel(...)
を使って追加しています。
public class AuxiliaryWindowsComboBoxUI extends WindowsComboBoxUI {
public static ComponentUI createUI(JComponent c) {
return new AuxiliaryWindowsComboBoxUI();
}
#spanadd
#spanend
@Override protected ComboPopup createPopup() {
return new BasicComboPopup2(comboBox);
}
#spanadd
#spanend
@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();
}
}
}
}
#spanadd
#spanend
//Override all UI-specific methods your UI classes inherit.
@Override public void removeEditor() {}
#spanadd
#spanend
@Override protected void configureEditor() {}
#spanadd
#spanend
@Override protected void unconfigureEditor() {}
#spanadd
#spanend
@Override public void update(Graphics g, JComponent c) {}
#spanadd
#spanend
@Override public void paint(Graphics g, JComponent c) {}
//...
// ...
- 注
-
LookAndFeel
をNimbus
にするとClassCastException
が発生する -
UIManager.addPropertyChangeListener(new PropertyChangeListener() {...});
を追加して、WindowsLookAndFeel
以外の場合は、UIManager.removeAuxiliaryLookAndFeel(auxLookAndFeel);
- 編集可能な
JComboBox
の場合、NullPointerException
が発生する-
WindowsComboBoxUI#addEditor()
をオーバーライド
-
-
-
LookAndFeel
をNimbus
にするとClassCastException
が発生する -
UIManager.addPropertyChangeListener(new PropertyChangeListener() {...});
を追加してWindowsLookAndFeel
以外の場合は、UIManager.removeAuxiliaryLookAndFeel(auxLookAndFeel);
- 編集可能な
JComboBox
の場合NullPointerException
が発生する-
WindowsComboBoxUI#addEditor()
をオーバーライド
-
参考リンク
- Using the Multiplexing Look and Feel
- 多重Look & Feelの使用
- UIManager#addAuxiliaryLookAndFeel(LookAndFeel) (Java Platform SE 8)