How to use the default file chooser for the operating system?
Use the old java.awt.FileDialog instead: new java.awt.FileDialog((java.awt.Frame) null).setVisible(true);
Use the old java.awt.FileDialog instead: new java.awt.FileDialog((java.awt.Frame) null).setVisible(true);
JFileChooser is compound JComponent, you can extract JButtons, JToggleButtons and JScrollPane with JViewPort that contains JList, please download Darryl’s Swing Utils , read descriptions, then run (Darryl’s) code, result is selection for JList or JTable (I voting for that) import java.awt.Color; import java.awt.Graphics; import javax.swing.*; import javax.swing.plaf.metal.MetalButtonUI; public class CrazyFileChooser { public static void main(String[] … Read more
You want to use a JFileChooser object. It will open and be modal, and block in the thread that opened it until you choose a file. Open: JFileChooser fileChooser = new JFileChooser(); if (fileChooser.showOpenDialog(modalToComponent) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); // load from file } Save: JFileChooser fileChooser = new JFileChooser(); if (fileChooser.showSaveDialog(modalToComponent) == … Read more
If I understand you correctly, you need to use the setSelectedFile method. JFileChooser jFileChooser = new JFileChooser(); jFileChooser.setSelectedFile(new File(“fileToSave.txt”)); jFileChooser.showSaveDialog(parent); The file doesn’t need to exist. If you pass a File with an absolute path, JFileChooser will try to position itself in that directory (if it exists).
I am putting a JFileChooser in my program, but that only takes images. For a list of types supported by that JRE on that OS, use ImageIO. FileFilter imageFilter = new FileNameExtensionFilter( “Image files”, ImageIO.getReaderFileSuffixes()); Types seen – Java 1.6/Windows 7 bmp jpg jpeg wbmp png gif Note: don’t hard-code that list! It might change … Read more
Incase anyone else needs this in the future: class DirectoryRestrictedFileSystemView extends FileSystemView { private final File[] rootDirectories; DirectoryRestrictedFileSystemView(File rootDirectory) { this.rootDirectories = new File[] {rootDirectory}; } DirectoryRestrictedFileSystemView(File[] rootDirectories) { this.rootDirectories = rootDirectories; } @Override public File createNewFolder(File containingDir) throws IOException { throw new UnsupportedOperationException(“Unable to create directory”); } @Override public File[] getRoots() { return rootDirectories; … Read more
Each time a new image is selected, you’re creating components unnecessarily and in error here: public void setTarget(File reference) { //…. panel_1.setLayout(new BorderLayout(0, 0)); panel_1.add(new JLabel(new ImageIcon(targetImg))); setVisible(true); Instead I would recommend that you have all these components created from the get-go, before any file/image has been selected, and then in this method, create an … Read more
As a general rule, you should not have your GUI classes, such as the class extending JPanel, implement any listener interfaces, and in fact you should strive for just the opposite — to separate the control functions of the program (the listeners and the like) from the view functions of the program (the GUI). So … Read more
The API for showOpenDialog() refers to showDialog(), which says, “If the parent is null, then the dialog depends on no visible window, and it’s placed in a look-and-feel-dependent position such as the center of the screen.” The example below positions the chooser in the center of the screen on my L&F. You might see how … Read more