AuxiliaryLookAndFeelを追加する
Total: 4963, Today: 1, Yesterday: 2
Posted by aterai at
Last-modified:
Summary
AuxiliaryLookAndFeelを追加して、WindowsLookAndFeelの場合の動作を変更します。
Screenshot

Advertisement
Source Code Examples
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, KotlinDescription
WindowsLookAndFeelの場合、それを修正することなくJComboBoxのドロップダウンリストで右クリックを無効にするようなComboBoxUIをUIManager.addAuxiliaryLookAndFeel(...)を使って追加しています。
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が発生するUIManager.addPropertyChangeListener(new PropertyChangeListener() {...});を追加してWindowsLookAndFeel以外の場合は、UIManager.removeAuxiliaryLookAndFeel(auxLookAndFeel);- 編集可能な
JComboBoxの場合NullPointerExceptionが発生するWindowsComboBoxUI#addEditor()をオーバーライド