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();
}
}
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,
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.
> 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.