FileDocCategorySizeDatePackage
FileChooserDemo.javaAPI DocExample16695Tue Jun 29 07:21:02 BST 1999None

FileChooserDemo

public class FileChooserDemo extends JPanel implements ActionListener
A demo which makes extensive use of the file chooser. 1.9 04/23/99
author
Jeff Dinkins

Fields Summary
static JFrame
frame
static String
metal
static String
metalClassName
static String
motif
static String
motifClassName
static String
windows
static String
windowsClassName
JButton
button
JCheckBox
useFileViewButton
JCheckBox
accessoryButton
JCheckBox
hiddenButton
JCheckBox
showFullDescriptionButton
JButton
noAllFilesFilterButton
JButton
yesAllFilesFilterButton
JRadioButton
addFiltersButton
JRadioButton
defaultFiltersButton
JRadioButton
openButton
JRadioButton
saveButton
JRadioButton
customButton
JRadioButton
metalButton
JRadioButton
motifButton
JRadioButton
windowsButton
JRadioButton
justFilesButton
JRadioButton
justDirectoriesButton
JRadioButton
bothFilesAndDirectoriesButton
JTextField
customField
ExampleFileFilter
jpgFilter
ExampleFileFilter
gifFilter
ExampleFileFilter
bothFilter
ExampleFileView
fileView
JPanel
buttonPanel
public static final Dimension
hpad10
public static final Dimension
vpad10
FilePreviewer
previewer
JFileChooser
chooser
Constructors Summary
public FileChooserDemo()


      
	setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

	chooser = new JFileChooser();
	previewer = new FilePreviewer(chooser);
	chooser.setAccessory(previewer);

	jpgFilter = new ExampleFileFilter("jpg", "JPEG Compressed Image Files");
	gifFilter = new ExampleFileFilter("gif", "GIF Image Files");
	bothFilter = new ExampleFileFilter(new String[] {"jpg", "gif"}, "JPEG and GIF Image Files");

	fileView = new ExampleFileView();
	fileView.putIcon("jpg", new ImageIcon("images/jpgIcon.jpg"));
	fileView.putIcon("gif", new ImageIcon("images/gifIcon.gif"));

	chooser.setAccessory(previewer);
	chooser.setFileView(fileView);

	// create a radio listener to listen to option changes
	OptionListener optionListener = new OptionListener();

	// Create options
	openButton = new JRadioButton("Open");
	openButton.setSelected(true);
	openButton.addActionListener(optionListener);

	saveButton = new JRadioButton("Save");
	saveButton.addActionListener(optionListener);

	customButton = new JRadioButton("Custom");
	customButton.addActionListener(optionListener);

	customField = new JTextField("Doit");
	customField.setAlignmentY(JComponent.TOP_ALIGNMENT);
	customField.setEnabled(false);
	customField.addActionListener(optionListener);

	ButtonGroup group1 = new ButtonGroup();
	group1.add(openButton);
	group1.add(saveButton);
	group1.add(customButton);

	// filter buttons
	noAllFilesFilterButton = new JButton("Remove \"All Files\" Filter");
	noAllFilesFilterButton.addActionListener(optionListener);

	yesAllFilesFilterButton = new JButton("Add \"All Files\" Filter");
	yesAllFilesFilterButton.addActionListener(optionListener);

	defaultFiltersButton = new JRadioButton("Default Filtering");
	defaultFiltersButton.setSelected(true);
	defaultFiltersButton.addActionListener(optionListener);

	addFiltersButton = new JRadioButton("Add JPG and GIF Filters");
	addFiltersButton.addActionListener(optionListener);

	ButtonGroup group2 = new ButtonGroup();
	group2.add(addFiltersButton);
	group2.add(defaultFiltersButton);

	accessoryButton = new JCheckBox("Show Preview");
	accessoryButton.addActionListener(optionListener);
	accessoryButton.setSelected(true);

	// more options
	hiddenButton = new JCheckBox("Show Hidden Files");
	hiddenButton.addActionListener(optionListener);

	showFullDescriptionButton = new JCheckBox("Show Extensions");
	showFullDescriptionButton.addActionListener(optionListener);
	showFullDescriptionButton.setSelected(true);

	useFileViewButton = new JCheckBox("Use FileView");
	useFileViewButton.addActionListener(optionListener);
	useFileViewButton.setSelected(true);

	// File or Directory chooser options
	ButtonGroup group3 = new ButtonGroup();
	justFilesButton = new JRadioButton("Just Select Files");
	justFilesButton.setSelected(true);
	group3.add(justFilesButton);
	justFilesButton.addActionListener(optionListener);

	justDirectoriesButton = new JRadioButton("Just Select Directories");
	group3.add(justDirectoriesButton);
	justDirectoriesButton.addActionListener(optionListener);

	bothFilesAndDirectoriesButton = new JRadioButton("Select Files or Directories");
	group3.add(bothFilesAndDirectoriesButton);
	bothFilesAndDirectoriesButton.addActionListener(optionListener);

	// Create show button
	button = new JButton("Show FileChooser");
	button.addActionListener(this);
        button.setMnemonic('s");

	// Create laf buttons.
	metalButton = new JRadioButton(metal);
        metalButton.setMnemonic('o");
	metalButton.setActionCommand(metalClassName);

	motifButton = new JRadioButton(motif);
        motifButton.setMnemonic('m");
	motifButton.setActionCommand(motifClassName);

	windowsButton = new JRadioButton(windows);
        windowsButton.setMnemonic('w");
	windowsButton.setActionCommand(windowsClassName);

	ButtonGroup group4 = new ButtonGroup();
	group4.add(metalButton);
	group4.add(motifButton);
	group4.add(windowsButton);

        // Register a listener for the laf buttons.
	metalButton.addActionListener(optionListener);
	motifButton.addActionListener(optionListener);
	windowsButton.addActionListener(optionListener);

	JPanel control1 = new JPanel();
	control1.setLayout(new BoxLayout(control1, BoxLayout.X_AXIS));
	control1.add(Box.createRigidArea(hpad10));
	control1.add(openButton);
	control1.add(Box.createRigidArea(hpad10));
	control1.add(saveButton);
	control1.add(Box.createRigidArea(hpad10));
	control1.add(customButton);
	control1.add(customField);
	control1.add(Box.createRigidArea(hpad10));

	JPanel control2 = new JPanel();
	control2.setLayout(new BoxLayout(control2, BoxLayout.X_AXIS));
	control2.add(Box.createRigidArea(hpad10));
	control2.add(noAllFilesFilterButton);
	control2.add(Box.createRigidArea(hpad10));
	control2.add(yesAllFilesFilterButton);
	control2.add(Box.createRigidArea(hpad10));
	control2.add(defaultFiltersButton);
	control2.add(Box.createRigidArea(hpad10));
	control2.add(addFiltersButton);
	control2.add(Box.createRigidArea(hpad10));
	control2.add(accessoryButton);
	control2.add(Box.createRigidArea(hpad10));

	JPanel control3 = new JPanel();
	control3.setLayout(new BoxLayout(control3, BoxLayout.X_AXIS));
	control3.add(Box.createRigidArea(hpad10));
	control3.add(hiddenButton);
	control3.add(Box.createRigidArea(hpad10));
	control3.add(showFullDescriptionButton);
	control3.add(Box.createRigidArea(hpad10));
	control3.add(useFileViewButton);
	control3.add(Box.createRigidArea(hpad10));

	JPanel control4 = new JPanel();
	control4.setLayout(new BoxLayout(control4, BoxLayout.X_AXIS));
	control4.add(Box.createRigidArea(hpad10));
	control4.add(justFilesButton);
	control4.add(Box.createRigidArea(hpad10));
	control4.add(justDirectoriesButton);
	control4.add(Box.createRigidArea(hpad10));
	control4.add(bothFilesAndDirectoriesButton);
	control4.add(Box.createRigidArea(hpad10));

	JPanel panel = new JPanel();
	panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
	panel.add(Box.createRigidArea(hpad10));
	panel.add(button);
	panel.add(Box.createRigidArea(hpad10));
	panel.add(metalButton);
	panel.add(Box.createRigidArea(hpad10));
	panel.add(motifButton);
	panel.add(Box.createRigidArea(hpad10));
	panel.add(windowsButton);
	panel.add(Box.createRigidArea(hpad10));

	add(Box.createRigidArea(vpad10));
	add(control1);
	add(Box.createRigidArea(vpad10));
	add(control2);
	add(Box.createRigidArea(vpad10));
	add(control3);
	add(Box.createRigidArea(vpad10));
	add(control4);
	add(Box.createRigidArea(vpad10));
	add(Box.createRigidArea(vpad10));
	add(panel);
	add(Box.createRigidArea(vpad10));
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

	int retval = chooser.showDialog(frame, null);
	if(retval == JFileChooser.APPROVE_OPTION) {
	    File theFile = chooser.getSelectedFile();
	    if(theFile != null) {
		if(theFile.isDirectory()) {
		    JOptionPane.showMessageDialog(
			frame, "You chose this directory: " +
			chooser.getSelectedFile().getAbsolutePath()
		    );
		} else {
		    JOptionPane.showMessageDialog(
			frame, "You chose this file: " +
			chooser.getSelectedFile().getAbsolutePath()
		    );
		}
		return;
	    }
	}
	JOptionPane.showMessageDialog(frame, "No file was chosen.");
    
public static voidmain(java.lang.String[] s)

	/*
	   NOTE: By default, the look and feel will be set to the
	   Cross Platform Look and Feel (which is currently Metal).
	   The user may someday be able to override the default
	   via a system property. If you as the developer want to
	   be sure that a particular L&F is set, you can do so
	   by calling UIManager.setLookAndFeel(). For example, the
	   first code snippet below forcibly sets the UI to be the
	   System Look and Feel. The second code snippet forcibly
	   sets the look and feel to the Cross Platform L&F.

	   Snippet 1:
	   try {
	      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	   } catch (Exception exc) {
	      System.err.println("Error loading L&F: " + exc);
	   }

	   Snippet 2:
	   try {
	      UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
	   } catch (Exception exc) {
	      System.err.println("Error loading L&F: " + exc);
	   }
	*/

	try {
	    // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
	} catch (Exception exc) {
	    System.err.println("Error loading L&F: " + exc);
	}

	FileChooserDemo panel = new FileChooserDemo();

	frame = new JFrame("FileChooserDemo");
	frame.addWindowListener(new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {System.exit(0);}
	});
	frame.getContentPane().add("Center", panel);
	frame.pack();
	frame.setVisible(true);

	panel.updateState();
    
public voidupdateState()

	String lnfName = UIManager.getLookAndFeel().getClass().getName();
	if (lnfName.indexOf(metal) >= 0) {
	    metalButton.setSelected(true);
	} else if (lnfName.indexOf(windows) >= 0) {
	    windowsButton.setSelected(true);
	} else if (lnfName.indexOf(motif) >= 0) {
	    motifButton.setSelected(true);
	} else {
	    System.err.println("FileChooserDemo if using an unknown L&F: " + lnfName);
	}