File Chooser Demo 2 : File Chooser « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » File ChooserScreenshots 
File Chooser Demo 2
File Chooser Demo 2
   
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.filechooser.FileView;

public class FileSample {

  public static void main(String args[]) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JLabel directoryLabel = new JLabel();
    contentPane.add(directoryLabel, BorderLayout.NORTH);

    final JLabel filenameLabel = new JLabel();
    contentPane.add(filenameLabel, BorderLayout.SOUTH);

    final JButton button = new JButton("Open FileChooser");
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        Component parent = (ComponentactionEvent.getSource();
        JFileChooser fileChooser = new JFileChooser(".");
        fileChooser.setAccessory(new LabelAccessory(fileChooser));
        FileView view = new JavaFileView();
        fileChooser.setFileView(view);
        int status = fileChooser.showOpenDialog(parent);
        if (status == JFileChooser.APPROVE_OPTION) {
          File selectedFile = fileChooser.getSelectedFile();
          directoryLabel.setText(selectedFile.getParent());
          filenameLabel.setText(selectedFile.getName());
        else if (status == JFileChooser.CANCEL_OPTION) {
          directoryLabel.setText(" ");
          filenameLabel.setText(" ");
        }
      }
    };
    button.addActionListener(actionListener);
    contentPane.add(button, BorderLayout.CENTER);

    frame.setSize(300200);
    frame.setVisible(true);
  }
}

class LabelAccessory extends JLabel implements PropertyChangeListener {
  private static final int PREFERRED_WIDTH = 125;

  private static final int PREFERRED_HEIGHT = 100;

  public LabelAccessory(JFileChooser chooser) {
    setVerticalAlignment(JLabel.CENTER);
    setHorizontalAlignment(JLabel.CENTER);
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
  }

  public void propertyChange(PropertyChangeEvent changeEvent) {
    String changeName = changeEvent.getPropertyName();
    if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
      File file = (FilechangeEvent.getNewValue();
      if (file != null) {
        ImageIcon icon = new ImageIcon(file.getPath());
        if (icon.getIconWidth() > PREFERRED_WIDTH) {
          icon = new ImageIcon(icon.getImage().getScaledInstance(
              PREFERRED_WIDTH, -1, Image.SCALE_DEFAULT));
          if (icon.getIconHeight() > PREFERRED_HEIGHT) {
            icon = new ImageIcon(icon.getImage().getScaledInstance(
                -1, PREFERRED_HEIGHT, Image.SCALE_DEFAULT));
          }
        }
        setIcon(icon);
      }
    }
  }
}

class JavaFileView extends FileView {
  Icon javaIcon = new DiamondIcon(Color.blue);

  Icon classIcon = new DiamondIcon(Color.green);

  Icon htmlIcon = new DiamondIcon(Color.red);

  Icon jarIcon = new DiamondIcon(Color.pink);

  public String getName(File file) {
    String filename = file.getName();
    if (filename.endsWith(".java")) {
      String name = filename + " : " + file.length();
      return name;
    }
    return null;
  }

  public String getTypeDescription(File file) {
    String typeDescription = null;
    String filename = file.getName().toLowerCase();

    if (filename.endsWith(".java")) {
      typeDescription = "Java Source";
    else if (filename.endsWith(".class")) {
      typeDescription = "Java Class File";
    else if (filename.endsWith(".jar")) {
      typeDescription = "Java Archive";
    else if (filename.endsWith(".html"|| filename.endsWith(".htm")) {
      typeDescription = "Applet Loader";
    }
    return typeDescription;
  }

  public Icon getIcon(File file) {
    if (file.isDirectory()) {
      return null;
    }
    Icon icon = null;
    String filename = file.getName().toLowerCase();
    if (filename.endsWith(".java")) {
      icon = javaIcon;
    else if (filename.endsWith(".class")) {
      icon = classIcon;
    else if (filename.endsWith(".jar")) {
      icon = jarIcon;
    else if (filename.endsWith(".html"|| filename.endsWith(".htm")) {
      icon = htmlIcon;
    }
    return icon;
  }
}

class DiamondIcon implements Icon {
  private Color color;

  private boolean selected;

  private int width;

  private int height;

  private Polygon poly;

  private static final int DEFAULT_WIDTH = 10;

  private static final int DEFAULT_HEIGHT = 10;

  public DiamondIcon(Color color) {
    this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected) {
    this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected, int width, int height) {
    this.color = color;
    this.selected = selected;
    this.width = width;
    this.height = height;
    initPolygon();
  }

  private void initPolygon() {
    poly = new Polygon();
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    poly.addPoint(0, halfHeight);
    poly.addPoint(halfWidth, 0);
    poly.addPoint(width, halfHeight);
    poly.addPoint(halfWidth, height);
  }

  public int getIconHeight() {
    return height;
  }

  public int getIconWidth() {
    return width;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(color);
    g.translate(x, y);
    if (selected) {
      g.fillPolygon(poly);
    else {
      g.drawPolygon(poly);
    }
    g.translate(-x, -y);
  }
}



           
         
    
    
  
Related examples in the same category
1. JFileChooser is a standard dialog for selecting a file from the file system.
2. A demo which makes extensive use of the file chooserA demo which makes extensive use of the file chooser
3. Demonstration of File dialog boxesDemonstration of File dialog boxes
4. JFileChooser class in action with an accessoryJFileChooser class in action with an accessory
5. A simple file chooser to see what it takes to make one of these workA simple file chooser to see what it takes to make one of these work
6. see what it takes to make one of these filters worksee what it takes to make one of these filters work
7. show thumbnails of graphic filesshow thumbnails of graphic files
8. FileChooser file filter customized filechooserFileChooser file filter customized filechooser
9. Choose a FileChoose a File
10. Customizing a JFileChooserCustomizing a JFileChooser
11. A simple FileFilter class that works by filename extension
12. Swing File Chooser DemoSwing File Chooser Demo
13. FileChooser DemoFileChooser Demo
14. File Chooser Demo 4File Chooser Demo 4
15. Getting the File-Type Icon of a File
16. Getting the Large File-Type Icon of a File
17. Listening for Approve and Cancel Events in a JFileChooser Dialog
18. String javax.swing.JFileChooser.APPROVE_SELECTION
19. void javax.swing.JFileChooser.addActionListener(ActionListener l)
20. JDialog javax.swing.JFileChooser.createDialog(Component parent)
21. void javax.swing.JFileChooser.setDialogType(int dialogType)
22. Changing the Text of the Approve Button in a JFileChooser Dialog
23. Determining If a File Is Hidden
24. Showing Hidden Files in a JFileChooser Dialog
25. Enabling Multiple Selections in a JFileChooser Dialog
26. Listening for Changes to the Selected File in a JFileChooser Dialog
27. Getting the File-Type Name of a File
28. Getting and Setting the Selected File of a JFileChooser Dialog
29. Getting and Setting the Current Directory of a JFileChooser Dialog
30. Determining If the Approve or Cancel Button Was Clicked in a JFileChooser Dialog
31. Adding a Filter to a File Chooser Dialog
32. Displaying Only Directories in a File Chooser Dialog
33. Creating a JFileChooser Dialog
34. Localize a JFileChooser
35. Select a directory with a JFileChooser
36. Disable the JFileChooser's "New folder" button
37. Validate a filename from a JFileChooser
38. extends FileView to create custom File chooser
39. Suffix FileFilter
40. Extension File Filter
41. Files filter that filters files by their suffix (or extension)
42. Generic File Filter
43. Get Directory Choice
44. Get File Choice
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.