3 Replies Latest reply: Jan 25, 2011 10:43 AM by 800119 RSS

    why can't I set location of FileDialog?

    795478
      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
        • 1. Re: why can't I set location of FileDialog?
          Darryl Burke
          Moderator action: Moved from Swing

          db
          • 2. Re: why can't I set location of FileDialog?
            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);
                }
            
            }
            • 3. Re: why can't I set location of FileDialog?
              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.