This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

why can't I set location of FileDialog?

ilantal
ilantal Member Posts: 164
edited Jan 25, 2011 11:43AM in Abstract Window Toolkit (AWT)
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

Answers

  • darrylburke
    darrylburke Member Posts: 18,007
    Moderator action: Moved from Swing

    db
  • 830031
    830031 Member Posts: 17
    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
    800119 Member Posts: 42
    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.
This discussion has been closed.