-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
|
||
} |