开发者

Set the Location of the JFileChooser

How can we set the location of the JFileCh开发者_如何学运维ooser window, I tried setLocation() and setBounds() methods but it doesn't works.


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.


From 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.


You could try to set the parent of your JFileChooser to your current JFrame/JPanel.

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

This should do the trick.

You can also look here for reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜