Java – set the location of Jfilechooser

How do we set the location of the Jfilechooser window? I tried the setlocation () and setbounds () methods, but it didn't work

Solution

Unfortunately, there is no easy way to do this, because whenever a selector is displayed, the internal createdialog method sets the location to the center of the parent

One method is to inherit Jfilechooser and override the createdialog method, as shown below:

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 use mychooser directly instead of Jfilechooser In the above code, I have hard coded the location as 20,20, but you can set it to anything you want

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>