package gui1;
/*
* BildChooserDemo.java
*
* [url]http://java.sun.com/docs/books/tutorial/uiswing/examples/components/[/url]
* FileChooserDemo2
*/
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.*;
public class BildChooserDemo extends JFrame {
private JButton btOpen;
private JToolBar toolBar;
private JFileChooser fc;
private File input;
private String collectionDir = "c:\\Paint Collection";
public BildChooserDemo() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
toolBar = new JToolBar();
btOpen = new JButton("Bild Laden...");
btOpen.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
btOpenActionPerformed(evt);
}
});
toolBar.add(btOpen);
getContentPane().add(toolBar, BorderLayout.NORTH);
pack();
}
private void btOpenActionPerformed(final ActionEvent evt) {
//Set up the file chooser.
fc = new JFileChooser();
fc.setCurrentDirectory(new File(collectionDir));
//Add a custom file filter and disable the default
//(Accept All) file filter.
fc.addChoosableFileFilter(new ImageFilter());
fc.setAcceptAllFileFilterUsed(false);
//Add the preview pane.
fc.setAccessory(new ImagePreview(fc));
//Show it.
int returnVal = fc.showDialog(this, "Laden");
//Process the results.
if (returnVal == JFileChooser.APPROVE_OPTION) {
input = fc.getSelectedFile();
System.out.println(input);
//loadImage(input);
}
}
public static void main(final String args[]) {new BildChooserDemo().setVisible(true);}
}
class ImageFilter extends FileFilter {
//Accept all directories and all gif, jpg, tiff, or png files.
public boolean accept(final File f) {
if (f.isDirectory()) {
return true;
}
String extension = Utils.getExtension(f);
if (extension != null) {
if (extension.equals(Utils.tiff) ||
extension.equals(Utils.tif) ||
extension.equals(Utils.gif) ||
extension.equals(Utils.jpeg) ||
extension.equals(Utils.jpg) ||
extension.equals(Utils.bmp) ||
extension.equals(Utils.png)) {
return true;
} else {
return false;
}
}
return false;
}
//The description of this filter
public String getDescription() {
return "Just Images";
}
}
class ImagePreview extends JComponent
implements PropertyChangeListener {
Image image = null;
File file = null;
public ImagePreview(final JFileChooser fc) {
setPreferredSize(new Dimension(100, 50));
fc.addPropertyChangeListener(this);
}
public void propertyChange(final PropertyChangeEvent e) {
boolean update = false;
String prop = e.getPropertyName();
//If the directory changed, don't show an image.
if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
file = null;
update = true;
//If a file became selected, find out which one.
} else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
file = (File) e.getNewValue();
update = true;
}
//Update the preview accordingly.
if (update) {
image = null;
if (isShowing()) {
repaint();
}
}
}
protected void paintComponent(final Graphics g) {
if( image == null && file != null ) {
//load image
try{ image = ImageIO.read(new File(file.getPath())); }catch(IOException ex){}
if (image != null) {
if (image.getWidth(null) > 90) {
image = image.getScaledInstance(90, -1, Image.SCALE_DEFAULT);
}
}
}
if (image != null) {
int x = getWidth()/2 - image.getWidth(null)/2;
int y = getHeight()/2 - image.getHeight(null)/2;
if (y < 0) {
y = 0;
}
if (x < 5) {
x = 5;
}
g.drawImage(image, x, y, null);
}
}
}
class Utils {
public final static String jpeg = "jpeg";
public final static String jpg = "jpg";
public final static String bmp = "bmp";
public final static String gif = "gif";
public final static String tiff = "tiff";
public final static String tif = "tif";
public final static String png = "png";
/*
* Get the extension of a file.
*/
public static String getExtension(final File f) {
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
}
return ext;
}
}