How can we set the location of the JFileChooser
window, I tried setLocation()
and setBounds()
methods but it doesn't works.
JFileChooser is a part of java Swing package. The java Swing package is part of JavaTM Foundation Classes(JFC) . JFC contains many features that help in building graphical user interface in java . Java Swing provides components such as buttons, panels, dialogs, etc .
Commonly used Constructors: Constructs a JFileChooser pointing to the user's default directory. Constructs a JFileChooser using the given File as the path. Constructs a JFileChooser using the given path.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With