I would like to be able to control the appearance of the JFileChooser
. In particular I would like to save how the JFileChooser
was displayed when it was last shown. I would like to save whether it was used in details/list view and which column (eg, size or date modified) the lists were sorted.
I know there is a lot of questions about JFileChooser
but I have not been able to find what I am looking for.
Thanks
EDIT: it was suggested as an answer, but keeping a reference of the filechooser is not suffient, as I want to persist the settings across many times that I run the application
EDIT: for example I usually want to open the most recent file I have downloaded, so I want to sort by date modified and display in detail view
How about Object Serialization ? You can Persist the JFileChooser Object to a file and later retrieve it.
Keep a reference to it and only construct it once. It should open on subsequent occasions looking pretty much like it did when the user disposed it. You would need to take extra steps to restore the location of the file chooser.
Persisting between runs.
There are a number of ways of storing the data between runs (e.g. a properties file, XML, Preferences
etc.). This is the quick'n'dirty way to achieve 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();
}
}
}
});
}
}
Correct I/O & exception handling is left as an exercise for the user.
Unfortunately, what you want to do is not trivial. It should be, but it's not implemented that way. The display of the directory and the sorting is part of the LAF. The only way to get the behavior you want is by implementing your own BasicFileChooserUI, overriding create
/getModel
and providing an implementation of BasicDirectoryModel. The sort method of BasicDirectoryModel is where the sorting is actually performed.
I've heard praise of XFileDialog, but haven't tried it myself. It's something to investigate, keeping in mind it is Windows-only (falls back to JFileChooser on OSX or Linux).