JDK-6342301 : Bad interaction between setting the ui and file filters in JFileChooser
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-10-26
  • Updated: 2011-03-08
  • Resolved: 2011-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b128Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
There appears to be an undocumented bad intereaction between
explicitely setting the UI and removing all file filters, even temporarily.
If the latter is done before setting the ui, trying to display a file dialog
will throw an exception but not if the ui was set prior to messing with
the filters. Maybe it is possible to make the code more robust against
this or to include a warning in the docs?

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached program: it will not bomb. Then move the setUI
line to the bottom of the constructor and try again: it will.


ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.NullPointerException
	at javax.swing.plaf.metal.MetalFileChooserUI$IndentIcon.getIconWidth(MetalFileChooserUI.java:1628)
	at javax.swing.SwingUtilities.layoutCompoundLabelImpl(SwingUtilities.java:842)
	at javax.swing.SwingUtilities.layoutCompoundLabel(SwingUtilities.java:769)
	at javax.swing.plaf.basic.BasicLabelUI.layoutCL(BasicLabelUI.java:56)
	at javax.swing.plaf.basic.BasicLabelUI.getPreferredSize(BasicLabelUI.java:214)
	at javax.swing.JComponent.getPreferredSize(JComponent.java:1275)
	at javax.swing.plaf.basic.BasicComboBoxUI.getSizeForComponent(BasicComboBoxUI.java:1312)
	at javax.swing.plaf.basic.BasicComboBoxUI.getDisplaySize(BasicComboBoxUI.java:1275)
	at javax.swing.plaf.metal.MetalComboBoxUI.getMinimumSize(MetalComboBoxUI.java:181)
	at javax.swing.plaf.basic.BasicComboBoxUI.getPreferredSize(BasicComboBoxUI.java:959)
	at javax.swing.JComponent.getPreferredSize(JComponent.java:1275)
	at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:572)
	at java.awt.Container.preferredSize(Container.java:1178)
	at java.awt.Container.getPreferredSize(Container.java:1162)
	at javax.swing.JComponent.getPreferredSize(JComponent.java:1277)
	at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:577)
	at javax.swing.plaf.metal.MetalFileChooserUI.getPreferredSize(MetalFileChooserUI.java:1170)
	at javax.swing.JComponent.getPreferredSize(JComponent.java:1275)
	at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:572)
	at java.awt.Container.preferredSize(Container.java:1178)
	at java.awt.Container.getPreferredSize(Container.java:1162)
	at javax.swing.JComponent.getPreferredSize(JComponent.java:1277)
	at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:820)
	at java.awt.Container.preferredSize(Container.java:1178)
	at java.awt.Container.getPreferredSize(Container.java:1162)
	at javax.swing.JComponent.getPreferredSize(JComponent.java:1277)
	at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:572)
	at java.awt.Container.preferredSize(Container.java:1178)
	at java.awt.Container.getPreferredSize(Container.java:1162)
	at java.awt.Window.pack(Window.java:438)
	at javax.swing.JFileChooser.createDialog(JFileChooser.java:748)
	at javax.swing.JFileChooser.showDialog(JFileChooser.java:687)
	at javax.swing.JFileChooser.showOpenDialog(JFileChooser.java:599)
	at Bug7.main(Bug7.java:24)
Exception in thread "main"

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.plaf.metal.MetalFileChooserUI;
import javax.swing.filechooser.FileFilter;

public class Bug7 extends JFileChooser {

    private FileFilter any = new FileFilter() {
	public boolean accept(File f) { return true; }
	public String getDescription() { return "Any file"; } };

    public Bug7() {
	super();
	setUI(new MetalFileChooserUI(this));
	// we remove all built-in file filters
	FileFilter[] ff = getChoosableFileFilters();
	for (int i = 0; i < ff.length; i++)
	    removeChoosableFileFilter(ff[i]);
	// we add a new one of our own, so the list is not empty
	addChoosableFileFilter(any);
    }

    public static final void main(String[] argv) {
	new Bug7().showOpenDialog(null);
    }
}

---------- END SOURCE ----------

Comments
EVALUATION The JFileChooser class doesn't update cached uiFileView when UI changes.
13-01-2011

EVALUATION Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?messageID=12632&forumID=1463
12-04-2006