[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
JavaFAQ_JFileChooser
This will depend on what you are using to view a file... For this [[sourcecode| code]], I will be using a JTextArea, and I will be loading Text Files into it.
I think you must have the JFileChooser Packages in the import list.
Also, because tokenizer is used, you must include ?Utils.* package.
Becuase we are using Exception handlers, we must have the IO package in the import list and [[IO.File]];
The import package list will look something like this:
I don't claim that this code is perfect, and it won't work that well for really large Text Files...only decently for files 15K and smaller.
See also ?java.io.*, ?java.io.File.*, ?java.util.*, java.swing.*, ?javax.swing.JFileChooser
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
JavaFAQ_JFileChooser
How to Load Files using JFileChooser
The following bit of code will open a Dialog ?window, that in turn will allow a user to search for a file and then by clicking on it and pressing the open button of the JFilechooser, they will load the file into your program.This will depend on what you are using to view a file... For this [[sourcecode| code]], I will be using a JTextArea, and I will be loading Text Files into it.
try
{
JFileChooser loadEmp = new JFileChooser();//new dialog
File selectedFile;//needed*
BufferedReader in;//needed*
FileReader reader = null;//needed*,look these three up for further info
//opens dialog if button clicked
if (loadEmp.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
//gets file from dialog
selectedFile = loadEmp.getSelectedFile();
//makes sure files can be processed before proceeding
if (selectedFile.canRead() && selectedFile.exists())
{
reader = new FileReader(selectedFile);
}
}
in = new BufferedReader(reader);
//inputLine recieves file text
String inputLine = in.readLine();
int LineNumber = 0;
while(inputLine!=null)
{
//LineNumber isn't needed, but it adds a line count on the left, nice
LineNumber++;
StringTokenizer tokenizer = new StringTokenizer(inputLine);
//displays text file
jTxtAMain.append(LineNumber+": "+inputLine+"\n");
//next line in File opened
inputLine = in.readLine();
}
//close stream, files stops loading
in.close();
}
//catches input/output exceptions and all subclasses exceptions
catch(IOException ex)
{
jTxtAMain.append("Error Processing File:\n" + ex.getMessage()+"\n");
}
//catches nullpointer exception, file not found
catch(NullPointerException ex)
{
jTxtAMain.append("Open File Cancelled:\n" + ex.getMessage()+"\n");
}
I think you must have the JFileChooser Packages in the import list.
Also, because tokenizer is used, you must include ?Utils.* package.
Becuase we are using Exception handlers, we must have the IO package in the import list and [[IO.File]];
The import package list will look something like this:
import java.io.*;
import java.io.File.*;
import java.util.*;
import javax.swing.JFileChooser;
I hope this helps someone..it did for me.
If anything in this code is inefficient, notify me before altering it.I don't claim that this code is perfect, and it won't work that well for really large Text Files...only decently for files 15K and smaller.
See also ?java.io.*, ?java.io.File.*, ?java.util.*, java.swing.*, ?javax.swing.JFileChooser
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
