Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

why can't I set location of FileDialog?

ilantalNov 25 2010 — edited Jan 25 2011
I have the following code:
FileDialog dlg = new FileDialog(parent, "Save MIP", FileDialog.SAVE);
dlg.setLocationRelativeTo(parent);
dlg.setVisible(true);
I can see under the debugger that dlg has a location of 0,0 before setLocationRelativeTo(parent) and afterwards has a location which looks reasonable to me. Still when I call setVisible, the dialog appears at 0,0. Any explanations or ideas what can be done?

Thanks,
Ilan

Comments

darrylburke
Moderator action: Moved from Swing

db
830031
You need to update graphics before making visible.

dlg.update(dlg.getGraphics());

Here is an example:
import javax.swing.*;
import java.awt.*;
public class Main {

    public static void main(String[] args)
    {
        Frame frame = new Frame();
        frame.setDefaultCloseOperation(
                JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class Frame extends JFrame
{
    public Frame()
    {
        setLocation(200,100);
        setTitle("My Frame");
        setSize(300,300);

        FileDialog dlg = new FileDialog(this, "Save MIP", FileDialog.SAVE);
        dlg.setLocation(this.getLocation());
        dlg.update(dlg.getGraphics());
        dlg.setVisible(true);
    }

}
800119
827028 wrote:
You need to update graphics before making visible.

dlg.update(dlg.getGraphics());
Sorry, but this is absolutely incorrect. Look up the Javadoc for Container#update() and Component#update() to see what that method does. It has nothing to do with positioning a dialog.

The likely reason the setLocationXXX() methods don't work with a FileDialog is because it's a truly "native" window, and is free to ignore your suggestions and behave as the OS sees fit. Here's a snippet from the java.awt.Window class Javadoc:

>
Note: the location and size of top-level windows (including Windows, Frames, and Dialogs) are under the control of the desktop's window management system. Calls to setLocation, setSize, and setBounds are requests (not directives) which are forwarded to the window management system. Every effort will be made to honor such requests. However, in some cases the window management system may ignore such requests, or modify the requested geometry in order to place and size the Window in a way that more closely matches the desktop settings.
1 - 3
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Feb 22 2011
Added on Nov 25 2010
3 comments
2,120 views