Skip to content

Commit

Permalink
Improved file chooser settings
Browse files Browse the repository at this point in the history
  • Loading branch information
litvinovg committed Jun 17, 2021
1 parent f90b1ec commit 5e79fd4
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/main/java/ru/ras/iph/impose/FileChooser.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
package ru.ras.iph.impose;

import java.awt.Dimension;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;
public class FileChooser extends JFrame {
private static String FILENAME_EXT = "pdf";
public static void main(String [] args){
chooseFile();
chooseFile("");
}
public static File chooseFile(){

public static File chooseFile(String lastPath){
File selected = null;

File currentDirectory = null;
if (lastPath != null && !lastPath.isEmpty()) {
File preselected = new File(lastPath);
if (preselected.exists()) {
if (preselected.isFile()) {
currentDirectory = preselected.getParentFile();
} else if (preselected.isDirectory()) {
currentDirectory = preselected;
}
}
}
if (currentDirectory == null) {
currentDirectory = new File(System.getProperty("user.home"));
}
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
fileChooser.setPreferredSize(new Dimension(800,600));
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
FileNameExtensionFilter filter = new FileNameExtensionFilter(FILENAME_EXT.toUpperCase(), FILENAME_EXT.toLowerCase());
fileChooser.setFileFilter(filter);
fileChooser.setCurrentDirectory(currentDirectory);
int result = fileChooser.showOpenDialog(null);
if (result == fileChooser.APPROVE_OPTION){
if (result == JFileChooser.APPROVE_OPTION){
selected = fileChooser.getSelectedFile();
System.out.println("Selected file " + selected.getAbsolutePath());
}
return selected;

}

}

0 comments on commit 5e79fd4

Please sign in to comment.