Scanner and JFileChooserBy: Nicholas Duchon |
Outline: |
public static void main(String[] args) throws Exception {
// an approach using javax with lambda expressions
javax.swing.SwingUtilities.invokeLater (() -> new Program());
// an approach using java with lambda expressions
java.awt.EventQueue.invokeAndWait (() -> new Program());
// an approach using an anonymous inner class
java.awt.EventQueue.invokeAndWait ( } // end method main
new Runnable() {
@Override
public void run() {
new Program();
} // end run method
} // end inner class definition
); // end invokeAndWait method.
Here's an example using JFileChooser:
// File: ChooserExample.java
// Date: Apr 30, 2010
// Author: Nicholas Duchon
// Purpose: Demonstrate JFileChooser
// reading a file with Scanner
// starting chooser in current directory
import java.io.FileNotFoundException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class ChooserExample
{
public static
void main (String args []) {
// sometimes starting with
JFileChooser can be erratic,
// so the following seems
to be a reliable way to start:
// for some bizarre reason,
sometimes I have had to do this more than once:
// start the search in the
current directory
// if this is not showing,
try to move the next line to a static variable outside main?
JFileChooser chooser = new
JFileChooser(".");
int returnVal =
chooser.showOpenDialog(null);
if(returnVal ==
JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
// open
and read file:
try {
Scanner sfin = new Scanner (
while (sfin.hasNext())
System.out.println (sfin.nextLine());
}
catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null,
"File not found.");
} // end try/catch
} // end if file chosen
} // end
main
} // end file
ChooserExample
Here's some very simple code to open a file, pestering the user until the open succeeds.
NOTE: The "FileNotFoundException" is thrown by the Scanner constructor, NOT by the File constructor!
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
public class GetFile {
public static void main (String args []) {
Scanner sin = new Scanner (System.in);
Scanner fin = null;
String fname = "";
while (fin == null) {
System.out.print ("File name: ");
fname = sin.next(); // get file name
try {
fin = new Scanner (new File (fname));
} catch (FileNotFoundException e) {
System.out.println ("File not found: " + fname);
}
} // end until good file found
System.out.println ("File open ok: " + fname);
} // end main
} // end class GetFile
// File: ChooserExample.java // Date: Apr 30, 2010 // Author: Nicholas Duchon // Purpose: Demonstrate JFileChooser // reading a file with Scanner // starting chooser in current directory import java.io.FileNotFoundException; import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.JOptionPane; import java.util.Scanner; public class ChooserExample { public static void main (String args []) { Scanner sfin = getScanner (); // call method to select and open file while (sfin.hasNext()) System.out.println (sfin.nextLine()); } // end main public static Scanner getScanner () { // start the search in the current directory JFileChooser chooser = new JFileChooser("."); // Start selection at dot! FileNameExtensionFilter filter; filter = new FileNameExtensionFilter("txt", "txt"); chooser.setFileFilter(filter); chooser.addChoosableFileFilter (new FileNameExtensionFilter("java", "java")); chooser.addChoosableFileFilter (new FileNameExtensionFilter("txt, java and class", "txt", "java", "class")); int returnVal = chooser.showOpenDialog(null); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); // attach Scanner to open and read file: try { return new Scanner (chooser.getSelectedFile()); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "File not found."); } // end try/catch } // end if file chosen return null; } // end method getScanner } // end file ChooserExample
Using regular expressions to define delimiters in Scanners: