1 /* FileChooserDemo.java -- An example showing file choosers in Swing.
2 Copyright (C) 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 package gnu.classpath.examples.swing;
24 import java.awt.BorderLayout;
25 import java.awt.GridLayout;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
30 import javax.swing.BorderFactory;
31 import javax.swing.DefaultListModel;
32 import javax.swing.JButton;
33 import javax.swing.JComponent;
34 import javax.swing.JFileChooser;
35 import javax.swing.JFrame;
36 import javax.swing.JLabel;
37 import javax.swing.JList;
38 import javax.swing.JPanel;
39 import javax.swing.JScrollPane;
40 import javax.swing.SwingUtilities;
41 import javax.swing.filechooser.FileFilter;
44 * A simple demo showing the {@link JFileChooser} component used in different
47 public class FileChooserDemo
49 implements ActionListener
52 * A file filter for Java source files.
54 static class JavaFileFilter extends FileFilter
56 public String getDescription()
58 return "Java Source Files (.java)";
60 public boolean accept(File f)
64 return f.getName().endsWith(".java") || f.isDirectory();
71 /** A label to display the selected file. */
72 JLabel selectedFileLabel;
75 * A list showing the selected files (where multi selections are
78 JList selectedFilesList;
80 /** A label to display the return code for the JFileChooser. */
81 JLabel returnCodeLabel;
84 * Creates a new demo instance.
86 public FileChooserDemo()
93 * When the demo is run independently, the frame is displayed, so we should
94 * initialise the content panel (including the demo content and a close
95 * button). But when the demo is run as part of the Swing activity board,
96 * only the demo content panel is used, the frame itself is never displayed,
97 * so we can avoid this step.
99 void initFrameContent()
101 JPanel closePanel = new JPanel();
102 JButton closeButton = new JButton("Close");
103 closeButton.setActionCommand("CLOSE");
104 closeButton.addActionListener(this);
105 closePanel.add(closeButton);
106 add(closePanel, BorderLayout.SOUTH);
110 * Returns a panel with the demo content. The panel
111 * uses a BorderLayout(), and the BorderLayout.SOUTH area
112 * is empty, to allow callers to add controls to the
113 * bottom of the panel if they want to (a close button is
114 * added if this demo is being run as a standalone demo).
116 private void createContent()
118 setLayout(new BorderLayout());
120 // create a panel of buttons to select the different styles of file
122 JPanel buttonPanel = new JPanel(new GridLayout(5, 1));
123 JButton openButton = new JButton("Open...");
124 openButton.setActionCommand("OPEN");
125 openButton.addActionListener(this);
126 buttonPanel.add(openButton);
127 JButton saveButton = new JButton("Save...");
128 saveButton.setActionCommand("SAVE");
129 saveButton.addActionListener(this);
130 buttonPanel.add(saveButton);
131 JButton queryButton = new JButton("Select Directory...");
132 queryButton.setActionCommand("SELECT_DIRECTORY");
133 queryButton.addActionListener(this);
134 buttonPanel.add(queryButton);
135 JButton openJavaButton = new JButton("Open Java file...");
136 openJavaButton.setActionCommand("OPEN_JAVA");
137 openJavaButton.addActionListener(this);
138 buttonPanel.add(openJavaButton);
139 JButton openMultiButton = new JButton("Open multiple files...");
140 openMultiButton.setActionCommand("OPEN_MULTI");
141 openMultiButton.addActionListener(this);
142 buttonPanel.add(openMultiButton);
143 add(buttonPanel, BorderLayout.WEST);
145 // create a panel to display the selected file(s) and the return code
146 JPanel displayPanel = new JPanel(new BorderLayout());
148 selectedFileLabel = new JLabel("-");
149 selectedFileLabel.setBorder(BorderFactory.createTitledBorder("Selected File/Directory: "));
150 displayPanel.add(selectedFileLabel, BorderLayout.NORTH);
152 selectedFilesList = new JList();
153 JScrollPane sp = new JScrollPane(selectedFilesList);
154 sp.setBorder(BorderFactory.createTitledBorder("Selected Files: "));
155 displayPanel.add(sp);
157 returnCodeLabel = new JLabel("0");
158 returnCodeLabel.setBorder(BorderFactory.createTitledBorder("Return Code:"));
159 displayPanel.add(returnCodeLabel, BorderLayout.SOUTH);
165 * When the user clicks on a button, launch the appropriate file chooser
166 * and report the results.
168 * @param e the event.
170 public void actionPerformed(ActionEvent e)
173 File selectedFile = null;
174 File[] selectedFiles = new File[0];
176 if (e.getActionCommand().equals("CLOSE"))
180 else if (e.getActionCommand().equals("OPEN"))
182 JFileChooser chooser = new JFileChooser();
183 option = chooser.showOpenDialog(this);
184 selectedFile = chooser.getSelectedFile();
185 selectedFiles = chooser.getSelectedFiles();
187 else if (e.getActionCommand().equals("OPEN_MULTI"))
189 JFileChooser chooser = new JFileChooser();
190 chooser.setMultiSelectionEnabled(true);
191 option = chooser.showOpenDialog(this);
192 selectedFile = chooser.getSelectedFile();
193 selectedFiles = chooser.getSelectedFiles();
195 else if (e.getActionCommand().equals("OPEN_JAVA"))
197 JFileChooser chooser = new JFileChooser();
198 chooser.setAcceptAllFileFilterUsed(false);
199 chooser.setFileFilter(new JavaFileFilter());
200 option = chooser.showOpenDialog(this);
201 selectedFile = chooser.getSelectedFile();
202 selectedFiles = chooser.getSelectedFiles();
204 else if (e.getActionCommand().equals("SAVE"))
206 JFileChooser chooser = new JFileChooser();
207 option = chooser.showSaveDialog(this);
208 selectedFile = chooser.getSelectedFile();
209 selectedFiles = chooser.getSelectedFiles();
211 else if (e.getActionCommand().equals("SELECT_DIRECTORY"))
213 JFileChooser chooser = new JFileChooser();
214 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
215 option = chooser.showDialog(this, "Select");
216 selectedFile = chooser.getSelectedFile();
217 selectedFiles = chooser.getSelectedFiles();
220 // display the selection and return code
221 if (selectedFile != null)
222 selectedFileLabel.setText(selectedFile.toString());
224 selectedFileLabel.setText("null");
225 DefaultListModel listModel = new DefaultListModel();
226 for (int i = 0; i < selectedFiles.length; i++)
227 listModel.addElement(selectedFiles[i]);
228 selectedFilesList.setModel(listModel);
229 returnCodeLabel.setText(Integer.toString(option));
232 public static void main(String[] args)
234 SwingUtilities.invokeLater
239 FileChooserDemo app = new FileChooserDemo();
240 app.initFrameContent();
241 JFrame frame = new JFrame("FileChooser Demo");
242 frame.getContentPane().add(app);
244 frame.setVisible(true);
250 * Returns a DemoFactory that creates a FileChooserDemo.
252 * @return a DemoFactory that creates a FileChooserDemo
254 public static DemoFactory createDemoFactory()
256 return new DemoFactory()
258 public JComponent createDemo()
260 return new FileChooserDemo();