Make JList Recalculate Component Widths and Heights
I have created a subclass of JList in which every item in the list has an associated icon and the text for the item is displayed underneath the icon. The JList is given the HORIZONTAL_WRAP layout orientation by default. I have an option to show the icon and the text or just the icon. When I change this property, I have the JList refresh itself to redraw the JLabels at their new preferred sizes. Currently I do it like this:
setVisibleRowCount(getVisibleRowCount()+1);
setVisibleRowCount(getVisibleRowCount()-1);
I'm hoping there's a better way. The repaint() method makes the text appear or disappear, but does not adjust the size of the list items until the window is resized. I tried invalidate(), revalidate(), and validate(), but they had no effect whatsoever. I tried using updateUI(), which works the first time, but generates an exception if used more than once:
Exception in thread"AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicListUI$Handler.isNavigationKey(BasicListUI.java:2060)
at javax.swing.plaf.basic.BasicListUI$Handler.keyTyped(BasicListUI.java:1986)
at java.awt.AWTEventMulticaster.keyTyped(AWTEventMulticaster.java:182)
at java.awt.Component.processKeyEvent(Component.java:5509)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2713)
at java.awt.Component.processEvent(Component.java:5331)
at java.awt.Container.processEvent(Container.java:2010)
at java.awt.Component.dispatchEventImpl(Component.java:4021)
at java.awt.Container.dispatchEventImpl(Container.java:2068)
at java.awt.Component.dispatchEvent(Component.java:3869)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1810)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:672)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:920)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:798)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:636)
at java.awt.Component.dispatchEventImpl(Component.java:3907)
at java.awt.Container.dispatchEventImpl(Container.java:2068)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3869)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

