Code for a jFileChooser that just selects directories


Details:

This code initialises and pops up a jFileChooser that only shows / allows you to select directories.


Original author/s: Andy Evans
Original location/s:
License: None


Imports and instance variables:

	import javax.swing.JFileChooser;

Code:

	JFileChooser fd = new JFileChooser() {  
			// Only need this if you want to change behaviour of methods.
			protected JDialog createDialog(Component parent) throws HeadlessException {  
		        JDialog jfc = super.createDialog(parent);  
		        jfc.setLocation(0, 0);  
		        return jfc;  
		      }  
		    };  
		
	fd.setFileHidingEnabled(true);
	fd.setCurrentDirectory(directoryToDisplayInAFileObject);
	fd.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

	int clicked = fd.showSaveDialog(component);
	
	switch (clicked) {
		case JFileChooser.APPROVE_OPTION:
			break;
		case JFileChooser.CANCEL_OPTION:
			return;
		case JFileChooser.ERROR_OPTION:
			return;
	}

	directory = fd.getSelectedFile();