File Name Bean : Files « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » File Input Output » FilesScreenshots 
File Name Bean

/**
 @version 1.20 1999-09-24
 @author Cay Horstmann
 */

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.Serializable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FileNameBean extends JPanel implements Serializable {
  public FileNameBean() {
    dialogButton = new JButton("...");
    nameField = new JTextField("");

    chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
      public boolean accept(File f) {
        String name = f.getName().toLowerCase();
        return name.endsWith("." + defaultExtension|| f.isDirectory();
      }

      public String getDescription() {
        return defaultExtension + " files";
      }
    });

    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 100;
    gbc.weighty = 100;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.fill = GridBagConstraints.BOTH;
    add(nameField, gbc, 0011);
    dialogButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        showFileDialog();
      }
    });
    nameField.setEditable(false);
    gbc.weightx = 0;
    gbc.anchor = GridBagConstraints.EAST;
    gbc.fill = GridBagConstraints.NONE;
    add(dialogButton, gbc, 1011);
  }

  public void add(Component c, GridBagConstraints gbc, int x, int y, int w,
      int h) {
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = w;
    gbc.gridheight = h;
    add(c, gbc);
  }

  public void showFileDialog() {
    int r = chooser.showOpenDialog(null);
    if (r == JFileChooser.APPROVE_OPTION) {
      String name = chooser.getSelectedFile().getAbsolutePath();
      setFileName(name);
    }
  }

  public void setFileName(String newValue) {
    String oldValue = nameField.getText();
    firePropertyChange("fileName", oldValue, newValue);
    nameField.setText(newValue);
  }

  public String getFileName() {
    return nameField.getText();
  }

  public Dimension getMinimumSize() {
    return new Dimension(XMINSIZE, YMINSIZE);
  }

  public String getDefaultExtension() {
    return defaultExtension;
  }

  public void setDefaultExtension(String s) {
    defaultExtension = s;
  }

  private static final int XMINSIZE = 200;

  private static final int YMINSIZE = 20;

  private JButton dialogButton;

  private JTextField nameField;

  private JFileChooser chooser;

  private String defaultExtension = "gif";

  public static void main() {
    JFrame f = new JFrame();
    f.setContentPane(new FileNameBean());
        
  }
}
           
       
Related examples in the same category
1. Find out the directoryFind out the directory
2. Listing the Directory ContentsListing the Directory Contents
3. Random FileRandom File
4. Data fileData file
5. Output to a text File
6. Choose a FileChoose a File
7. Read data from text file
8. Copy File
9. Querying a File for Information
10. Working with RandomAccessFileWorking with RandomAccessFile
11. Get a list of files, and check if any files are missing
12. Delete a file from within Java
13. Work with temporary files in Java
14. Compare File Dates
15. Strings -- extract printable strings from binary file
w_w_w___.___j_a_va___2_s_.c___o_m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.