Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to prevent JFileChooser automatically changing to parent directory?

843806Jan 29 2009 — edited Jan 29 2009
When you show only directories, and click on the dir icons to navigate, and then dont select anything and click OK, it automatically 'cd's to the parent folder.
My application is using the JFileChooser to let the user navigate through folders and certain details of 'foo' files in that folder are displayed in another panel.
So we dont want the chooser automatically changing dir to parent when OK is clicked. How to prevent this behavior?
I considered extending the chooser and looked at the Swing source code but it is hard to tell where the change dir is happening.
thanks,
Anil

To demonstrate this, I took the standard JFileChooserDemo from the Sun tutorial and modified it adding these lines
		// NEW line 45 in constructor
		fc.addPropertyChangeListener((PropertyChangeListener) this);
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

	/*
	 * NEW -
	 * 
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void propertyChange(PropertyChangeEvent e) {
		String prop = e.getPropertyName();
		if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
			System.out.println("DIRECTORY_CHANGED_PROPERTY");
			File file = (File) e.getNewValue();
			System.out.println("DIRECTORY:" + file.getPath());
		}
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 26 2009
Added on Jan 29 2009
4 comments
495 views