Validate a filename from a JFileChooserTag(s): Swing


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;


public class DemoJFileChooser extends JPanel
   implements ActionListener {
   JButton go;

   JFileChooser chooser;
   String choosertitle;

  public DemoJFileChooser() {
    go = new JButton("Do it");
    go.addActionListener(this);
    add(go);
  }

  public void actionPerformed(ActionEvent e) {
    int result;

    chooser = new JFileChooser();
    chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle(choosertitle);
    //
    int rc = chooser.showOpenDialog(this);
    //
    // loop until  a .java file is selected
    //
    // possible  to perform complex validation
    // using a regular expression with .matches(regexp)
    //
    while(rc == JFileChooser.APPROVE_OPTION &&
       !chooser.getSelectedFile().getName().endsWith(".java")){
      JOptionPane.showMessageDialog(null,
       "The file " + chooser.getSelectedFile()
       + " is not java source file.", "Open Error",
       JOptionPane.ERROR_MESSAGE);
      rc = chooser.showOpenDialog(this);
    }


    if (rc == JFileChooser.APPROVE_OPTION) {
      System.out.println("getCurrentDirectory(): "
         +  chooser.getCurrentDirectory());
      System.out.println("getSelectedFile() : "
         +  chooser.getSelectedFile());
    }
    else {
      System.out.println("No Selection!");
    }
  }

  public Dimension getPreferredSize(){
    return new Dimension(200, 200);
  }

  public static void main(String s[]) {
    JFrame frame = new JFrame("");
    DemoJFileChooser panel = new DemoJFileChooser();
    frame.addWindowListener(
      new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      }
    );
    frame.getContentPane().add(panel,"Center");
    frame.setSize(panel.getPreferredSize());
    frame.setVisible(true);
  }
}  

blog comments powered by Disqus