//show file chooser dialog int result=chooser.showOpenDialog(FileChooserFrame.this); //if image file accepted ,set it as icon of the label if(result==JFileChooser.APPROVE_OPTION) { String name=chooser.getSelectedFile().getPath(); label.setIcon(new ImageIcon(name)); } } } public static final int WIDTH=300; public static final int HEIGHT=400;
private JLabel label; private JFileChooser chooser; } /** this file filter matches all files with a given set of extension */ class ExtensionFileFilter extends FileFilter { /** adds an extension that this file filter recognize @param extension a file extension(such as ".txt" or "txt") */ public void addExtension(String extension) { if(!extension.startsWith(".")) extension="."+extension; extensions.add(extension.toLowerCase()); } /** sets a description for the file set that this file filter recognize @param aDescriprtion a description for file set */ public void setDescription(String aDescription) { description=aDescription; } /** returns a description for the file set that this file filter recognizes @return a description for file set */ public String getDescription() { return description; } public boolean accept(File f) { if(f.isDirectory()) return true; String name =f.getName().toLowerCase();
//check if the file name ends with any of the extensions for(int i=0;i<extensions.size();i++)
} private String description=""; private ArrayList extensions=new ArrayList(); } /** a file view that display an icon for all files that match a file filter */ class FileIconView extends FileView { /** constructs a fileIconView @param aFilter a file filter--all files that this filter accepts will be shown with the icon @param anIcon--the icon shown with all accepted files */ public FileIconView(FileFilter aFilter,Icon anIcon) { filter=aFilter; icon=anIcon; } public Icon getIcon(File f) { if(!f.isDirectory()&& filter.accept(f)) return icon; else return null; } private FileFilter filter; private Icon icon; } /** a file chooser accessory that preview images */ class ImagePreviewer extends JLabel { /** constructs an ImagePreviewer @param chooser the file chooser whose property changes trigger an image change in this previewer */ public ImagePreviewer(JFileChooser chooser) { setPreferredSize(new Dimension(100,100)); setBorder(BorderFactory.createEtchedBorder());
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.beans.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;
public class FileChooserTest
{
public static void main(String args[])
{
FileChooserFrame frame=new FileChooserFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
}
class FileChooserFrame extends JFrame
{
public FileChooserFrame()
{
setTitle("FileChooserTest");
setSize(WIDTH,HEIGHT);
//set up menu bar
JMenuBar mbar=new JMenuBar();
setJMenuBar(mbar);
JMenu menu=new JMenu("File");
mbar.add(menu);
JMenuItem mItem=new JMenuItem("Open");
menu.add(mItem);
mItem.addActionListener(new FileOpenListener());
JMenuItem exitItem=new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
//uses a label display the images
label=new JLabel();
Container con=getContentPane();
con.add(label);
chooser =new JFileChooser();
}
private class FileOpenListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//set up file chooser
chooser.setCurrentDirectory(new File("."));
//accept all images files ending with .jpg,.jpeg,.gif
final ExtensionFileFilter filter=new ExtensionFileFilter();
filter.addExtension("jpg");
filter.addExtension("jpeg");
filter.addExtension("gif");
filter.setDescription("Image files");
chooser.setFileFilter(filter);
chooser.setAccessory(new ImagePreviewer(chooser));
//show file chooser dialog
int result=chooser.showOpenDialog(FileChooserFrame.this);
//if image file accepted ,set it as icon of the label
if(result==JFileChooser.APPROVE_OPTION)
{
String name=chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
}
public static final int WIDTH=300;
public static final int HEIGHT=400;
private JLabel label;
private JFileChooser chooser;
}
/**
this file filter matches all files with a given set of extension
*/
class ExtensionFileFilter extends FileFilter
{
/**
adds an extension that this file filter recognize
@param extension a file extension(such as ".txt" or "txt")
*/
public void addExtension(String extension)
{
if(!extension.startsWith("."))
extension="."+extension;
extensions.add(extension.toLowerCase());
}
/**
sets a description for the file set that this file filter recognize
@param aDescriprtion a description for file set
*/
public void setDescription(String aDescription)
{
description=aDescription;
}
/**
returns a description for the file set that this file filter recognizes
@return a description for file set
*/
public String getDescription()
{
return description;
}
public boolean accept(File f)
{
if(f.isDirectory()) return true;
String name =f.getName().toLowerCase();
//check if the file name ends with any of the extensions
for(int i=0;i<extensions.size();i++)
if(name.endsWith((String)extensions.get(i)))
return true;
return false;
}
private String description="";
private ArrayList extensions=new ArrayList();
}
/** a file view that display an icon for all files that match a file filter
*/
class FileIconView extends FileView
{
/**
constructs a fileIconView
@param aFilter a file filter--all files that this filter
accepts will be shown with the icon
@param anIcon--the icon shown with all accepted files
*/
public FileIconView(FileFilter aFilter,Icon anIcon)
{
filter=aFilter;
icon=anIcon;
}
public Icon getIcon(File f)
{
if(!f.isDirectory()&& filter.accept(f))
return icon;
else return null;
}
private FileFilter filter;
private Icon icon;
}
/**
a file chooser accessory that preview images
*/
class ImagePreviewer extends JLabel
{
/**
constructs an ImagePreviewer
@param chooser the file chooser whose property changes
trigger an image change in this previewer
*/
public ImagePreviewer(JFileChooser chooser)
{
setPreferredSize(new Dimension(100,100));
setBorder(BorderFactory.createEtchedBorder());
chooser.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent event)
{
if(event.getPropertyName()==JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)
{
File f=(File)event.getNewValue();
ImageIcon icon=new ImageIcon(f.getPath());
if(icon.getIconWidth()>getWidth())
icon=new ImageIcon(icon.getImage().getScaledInstance(getWidth(),-1,Image.SCALE_DEFAULT));
setIcon(icon);
}
}
});
}
}
//: FileDialogTest.java
// Demonstration of File dialog boxes
import java.awt.*;public class FileDialogTest extends Frame {
TextField filename = new TextField();
TextField directory = new TextField();
Button open = new Button("Open");
Button save = new Button("Save");
public FileDialogTest() {
setTitle("File Dialog Test");
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.add(open);
p.add(save);
add("South", p);
directory.setEditable(false);
filename.setEditable(false);
p = new Panel();
p.setLayout(new GridLayout(2,1));
p.add(filename);
p.add(directory);
add("North", p);
}
public boolean handleEvent(Event evt) {
if(evt.id == Event.WINDOW_DESTROY)
System.exit(0);
else
return super.handleEvent(evt);
return true;
}
public boolean action(Event evt, Object arg) {
if(evt.target.equals(open)) {
// Two arguments, defaults to open file:
FileDialog d = new FileDialog(this,
"What file do you want to open?");
d.setFile("*.java"); // Filename filter
d.setDirectory("."); // Current directory
d.show();
String openFile;
if((openFile = d.getFile()) != null) {
filename.setText(openFile);
directory.setText(d.getDirectory());
} else {
filename.setText("You pressed cancel");
directory.setText("");
}
}
else if(evt.target.equals(save)) {
FileDialog d = new FileDialog(this,
"What file do you want to save?",
FileDialog.SAVE);
d.setFile("*.java");
d.setDirectory(".");
d.show();
String saveFile;
if((saveFile = d.getFile()) != null) {
filename.setText(saveFile);
directory.setText(d.getDirectory());
} else {
filename.setText("You pressed cancel");
directory.setText("");
}
}
else
return super.action(evt, arg);
return true;
}
public static void main(String[] args) {
Frame f = new FileDialogTest();
f.resize(250,110);
f.show();
}
} ///:~