How can we set the location of the JFileChooser
window, I tried setLocation()
and setBounds()
methods but it doesn't works.
views:
192answers:
2From the JavaDoc for JFileChooser
's showDialog
, it looks as if you do not have a great amount of control over where the dialog gets placed.
The parent argument determines two things: the frame on which the open dialog depends and the component whose position the look and feel should consider when placing the dialog. If the parent is a Frame object (such as a JFrame) then the dialog depends on the frame and the look and feel positions the dialog relative to the frame (for example, centered over the frame). If the parent is a component, then the dialog depends on the frame containing the component, and is positioned relative to the component (for example, centered over the component). 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.
Unfortunatley there is no trivial way to do it, because whenever you show the chooser, the internal createDialog method will set the location to center of parent.
One way to do is to subclass JFileChooser and override createDialog method like this:
static class MyChooser extends JFileChooser {
protected JDialog createDialog(Component parent)
throws HeadlessException {
JDialog dlg = super.createDialog(parent);
dlg.setLocation(20, 20);
return dlg;
}
}
Now you can directly uise MyChooser instead of JFileChooser. In above code I have hardcoded the location to 20, 20, but you can set it to whatever you want.