Saving File Chooser settings

How would one go about saving a user's changes to the File Chooser dialog? Say that they have resized the screen and set the display mode to detailed. Can those items be saved off and used the next time a file chooser is invoked?Thanks.
[251 byte] By [pherigoa] at [2007-11-26 14:28:19]
# 1

As far as keeping the same size on your JFileChooser, here's an easy example.

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ComponentEvent;

import java.awt.event.ComponentAdapter;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

public class FileChooserSaveExample extends JFrame

{

//DATA

private JFileChooser fileChooser;

//GUI

private JButton openFileChooserButton;

public FileChooserSaveExample()

{

this.setSize(new Dimension(200,200));

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

this.setContentPane(getOpenFileChooserButton());

this.setVisible(true);

}

//DATA

private JFileChooser getFileChooser()

{

if(fileChooser == null)

{

fileChooser = new JFileChooser();

fileChooser.addComponentListener(new ComponentAdapter()

{

public void componentResized(ComponentEvent e)

{

fileChooser.setPreferredSize(new Dimension(fileChooser.getSize().width,fileChooser.getSize().height));

}

});

}

return fileChooser;

}

//GUI

private JButton getOpenFileChooserButton()

{

if(openFileChooserButton == null)

{

openFileChooserButton = new JButton("Open File Chooser");

openFileChooserButton.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

getFileChooser().showOpenDialog(FileChooserSaveExample.this);

}

});

}

return openFileChooserButton;

}

public static void main(String args[])

{

new FileChooserSaveExample();

}

}

JSnakea at 2007-7-8 2:22:21 > top of Java-index,Desktop,Core GUI APIs...
# 2

you can use java.util.prefs.Preferences. This will save the values in the system and you can get it when you need. These values will available even if you close and open the application again. The values can be save as user specific or system specific. refer the API for details.

Example code

final Preferences pref = Preferences.userRoot();

final JFileChooser jf = new JFileChooser();

jf.setPreferredSize(new Dimension(

// get the file chosser size which is already stored. If not exists then default size is 100.

pref.node("Window Settings").getInt("Width",100),

pref.node("Window Settings").getInt("Height",100)));

jf.addComponentListener(new ComponentListener() {

public void componentHidden(ComponentEvent e) {

}

public void componentMoved(ComponentEvent e) {

}

public void componentResized(ComponentEvent e) {

// store the size when it is resized

pref.node("Window Settings").putInt("Width",jf.getWidth());

pref.node("Window Settings").putInt("Height",jf.getHeight());

}

public void componentShown(ComponentEvent e) {

}

});

jf.showDialog(ft,"Approve");

Sunan.Na at 2007-7-8 2:22:21 > top of Java-index,Desktop,Core GUI APIs...
# 3

Sunan,

I liked your thoughts on this problem.

Do you know if these settings are saved in a user's home area, or could other users pick up the settings made by someone else? On a SUN system running UNIX, it said the preferences were in the user's preferences file, but then it gave the name of "absolute path" as being the root directory, "/". Unfortunately, the "absolute path" that Java returns doesn't tell me much about where Java is storing the information!

Thanks for your response.

pherigoa at 2007-7-8 2:22:21 > top of Java-index,Desktop,Core GUI APIs...
# 4
JSnake,Thanks for the prompt reply!A nice solution.
pherigoa at 2007-7-8 2:22:21 > top of Java-index,Desktop,Core GUI APIs...
# 5

> Do you know if these settings are saved in a user's

> home area, or could other users pick up the settings

> made by someone else?

If you use userroot() then other users cannot use that users settings.

You can use systemroot() to share between users.

> Unfortunately, the "absolute path" that Java returns

> doesn't tell me much about where Java is storing the

> information!

The absolute path will give the path of the node in the tree only. It will not give the path it is stored in the system.

I dont know where the informations are saved. please tell me if you could find.

Sunan.Na at 2007-7-8 2:22:21 > top of Java-index,Desktop,Core GUI APIs...
# 6
JSnake and Sunan:Hope you don't mind that I split the reward between the two of you. I haven't decided which path to go yet, or use some of each. Great answers and thanks alot for posting so quickly to the board!
pherigoa at 2007-7-8 2:22:21 > top of Java-index,Desktop,Core GUI APIs...