Set JFileChooser Location

How can we set the location of the window JFileChooser, I tried the methods setLocation()and setBounds(), but it does not work.

+5
source share
3 answers

Unfortunately, there is no trivial way to do this, because whenever you show a chooser, the internal createDialog method sets the location to the center of the parent.

One way to do this is to subclass JFileChooser and override the createDialog method as follows:

   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 the code above, I hardcoded the location to 20, 20, but you can configure it to whatever you want.

+8

JavaDoc JFileChooser showDialog, , , .

: , , , . Frame (, JFrame), , (, ). , , , (, ). NULL, , , , .

0

JFileChooser JFrame/JPanel.

JFrame parentToBe = new JFrame();
JFileChooser chooser = new JFileChooser();
int pathSelection = chooser.showSaveDialog(parentToBe);

.

.

0
source

All Articles