Java JFilechooser

I would like to be able to control the appearance of JFileChooser . In particular, I would like to keep the JFileChooser way of displaying when it was the last. I would like to keep whether it was used in the details / presentation of the list and in which column (for example, size or date was resized) the lists were sorted.

I know that there are many questions about JFileChooser , but I could not find what I was looking for.

thanks

EDIT: this was suggested as an answer, but saving a link to filechooser is not enough, since I want to save the settings many times to start the application

EDIT: for example, I usually want to open the most recently uploaded file, so I want to sort by date modified and displayed in the detailed view

+4
source share
3 answers

Keep a link to it and only construct it once. It should open in subsequent cases, looking pretty much like the user did. You will need to take additional steps to restore the file selection location .


Saving between starts.

There are several ways to store data between runs (for example, a properties file, XML, Preferences , etc.). This is a quick way to do it.

 import java.awt.event.*; import javax.swing.*; import java.io.*; class SerializeMyChooser { static private JFileChooser fileChooser; static File serializedChooser = new File("chooser.ser"); public static void main(String[] args) { SwingUtilities.invokeLater( new Runnable() { public void run() { final JButton showChooser = new JButton("Open File"); showChooser.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { if (fileChooser==null) { if (serializedChooser.exists()) { // use the resialized form try { ObjectInputStream ois = new ObjectInputStream( new FileInputStream(serializedChooser)); fileChooser = (JFileChooser)ois.readObject(); ois.close(); } catch(Exception e) { // something SNAFU - use fall-back fileChooser = new JFileChooser(); // configure file chooser.. } } else { fileChooser = new JFileChooser(); // configure file chooser.. } } fileChooser.showOpenDialog(showChooser); } }); JOptionPane.showMessageDialog(null, showChooser); if (fileChooser!=null) { try { ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(serializedChooser)); oos.writeObject(fileChooser); oos.flush(); oos.close(); } catch(Exception e) { e.printStackTrace(); } } } }); } } 

The correct handling of I / O and exception handling remains as an exercise for the user.

+2
source

How about serializing objects? You can transfer the JFileChooser object to a file and then get it.

+2
source

Unfortunately, what you want to do is not trivial. It should be, but it is not implemented that way. Catalog mapping and sorting are part of LAF. The only way to achieve the desired behavior is to implement your own BasicFileChooserUI by overriding create / getModel and providing an implementation of BasicDirectoryModel . The BasicDirectoryModel sorting method is where the sorting is actually performed.

I have heard the praise of XFileDialog , but have not tried it myself. This is something to investigate, bearing in mind that it is only Windows (returns to JFileChooser on OSX or Linux).

+1
source

All Articles