Java Utililty Methods JFileChooser

List of utility methods to do JFileChooser

Description

The list of methods to do JFileChooser are organized into topic(s).

Method

JFileChoosergetTextFileChooser()
Gets a text file chooser.
final JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text files", "txt");
chooser.setFileFilter(filter);
return chooser;
JFileChoosergetXmlFileChooser()
get Xml File Chooser
JFileChooser xmlFileChooser = new JFileChooser();
FileFilter filter = new FileFilter() {
    public boolean accept(File file) {
        if (file.isDirectory())
            return true;
        String name = file.getName().toLowerCase();
        return name.endsWith(".xml");
    public String getDescription() {
        return "XML";
};
xmlFileChooser.setFileFilter(filter);
return xmlFileChooser;
StringimportFile(JFileChooser jFileChooser)
import File
StringBuilder stringBuilder;
int result = jFileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
    final ArrayList<String> parfileTextLines = myReadDataFile(jFileChooser.getSelectedFile().toString());
    stringBuilder = new StringBuilder();
    for (String currLine : parfileTextLines) {
        stringBuilder.append(currLine);
        stringBuilder.append("\n");
...
JFileChooserinitFileChooser(JFileChooser fileChooser, FileFilter filter)
init File Chooser
if (fileChooser.getSelectedFile() != null) { 
    fileChooser = new JFileChooser(fileChooser.getSelectedFile().getPath());
if (filter != null) {
    fileChooser.setFileFilter(filter);
return fileChooser;
booleanisDriveTraversable(File drive)
is Drive Traversable
return fsv.isTraversable(drive);
StringloadFile()
load File
JFileChooser fileChooser = new JFileChooser(lastTouchedDirectory);
fileChooser.setApproveButtonText("Load");
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
    File selectedFile = fileChooser.getSelectedFile();
    lastTouchedFileName = selectedFile.getName();
    lastTouchedDirectory = selectedFile.getParent();
    return loadFile(selectedFile);
return null;
java.io.FileloadFile(Component parent, String title)
This method is used to choose a file path.
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("./"));
chooser.setDialogTitle(title);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setVisible(true);
if (chooser.showOpenDialog(chooser) == JFileChooser.APPROVE_OPTION) {
    return chooser.getSelectedFile();
return null;
TloadFileAs(Class clazz, String json)
load File As
try {
    return mapper.readValue(json, clazz);
} catch (IOException e) {
    e.printStackTrace();
    return null;
JFileChoosermakeFileChooser()
make File Chooser
final JFileChooser temp = new JFileChooser();
final String dir = System.getProperty("user.dir");
final File fileDir = new File(dir);
temp.setCurrentDirectory(fileDir);
return temp;
JFileChooseropenDataFileChooser(Component com)
File chooser to read data files.
JFileChooser fileChooser = new JFileChooser();
fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.addChoosableFileFilter(
        new FileNameExtensionFilter("All *.jser,*.jxml,*.jpbu,*.jdat,*.root,*.aida,*.xml", "jser", "jxml",
                "jpbu", "jdat", "root", "aida", "xml"));
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("HFile (*.jser)", "jser"));
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("HFileXML (*.jxml)", "jxml"));
...