Changing PLAF when a JComboBox is open?

I am trying to write a simple way of changing the look and feel that I can easily connect to most of the possible ways that someone might want to change the plaf (for example combo box, list, radio buttons, menus & popups etc)

The best way of doing this I have found so far is implementing ItemListener. I can just get the contents of the event when it is ItemEvent.SELECTED and change the PLAF withUIManager.setLookAndFeel(lookAndFeel);

SwingUtilities.updateComponentTreeUI(c);

where "c" was the root component to change from, given to my class.

This works fine in most cases but when using the keyboard to navigate a JComboBox, the item can be selected while the drop down list is still open. This causes the updateComponentTreeUI to error:Exception in thread"AWT-EventQueue-0" java.lang.NullPointerException

at javax.swing.plaf.basic.BasicComboBoxUI.selectNextPossibleValue(BasicComboBoxUI.java:1072)

at javax.swing.plaf.basic.BasicComboBoxUI$Actions.actionPerformed(BasicComboBoxUI.java:1432)

at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)

at javax.swing.JComponent.processKeyBinding(JComponent.java:2844)

at javax.swing.JComponent.processKeyBindings(JComponent.java:2890)

at javax.swing.JComponent.processKeyEvent(JComponent.java:2807)

at javax.swing.JComboBox.processKeyEvent(JComboBox.java:1399)

at java.awt.Component.processEvent(Component.java:5815)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)

at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:693)

at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:958)

at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:830)

at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:657)

at java.awt.Component.dispatchEventImpl(Component.java:4282)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Window.dispatchEventImpl(Window.java:2429)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

Is there a safe way of doing this or a way of avoiding the errors? Would somehow using SwingUtilities.invokeLater(Runnable) help? Is there a better way of doing what I want without using ItemListener etc?

[3275 byte] By [Ghelyara] at [2007-11-27 4:52:46]
# 1
I am no expert on this, but would a try/catch block work at preventing changes in the LAF at inopportune moments?
petes1234a at 2007-7-12 10:06:54 > top of Java-index,Desktop,Core GUI APIs...
# 2
Wrap you code from the ItemListener is a SwingUtilities.invokeLater(...). This will cause the code to be executed after the combo box drop down has been closed.
camickra at 2007-7-12 10:06:54 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks, this seems to have worked well enough, though I don't know if I am going about this the correct way.

I changed

SwingUtilities.updateComponentTreeUI(c);

into

SwingUtilities.invokeLater(new Runnable() {

public void run() {

SwingUtilities.updateComponentTreeUI(c);

}

});

Which stops the error but doesn't cause the code only to be executed after the drop down popup has been changed, when using the keyboard down cursor it lets you go down 1 item then closes the drop down and changes the plaf. Normally when I use the down cursor on JComboBoxes the box does not disappear when switching between things.

I can get it to stay open and change the look and feel but as soon as I add the update component tree UI even wrapped in an invokeLater, it closes the dropdown popup and changes it.

Ghelyara at 2007-7-12 10:06:54 > top of Java-index,Desktop,Core GUI APIs...