views:

194

answers:

1

I have never been able to figure this one out; the usual suspects don't work.

Given:

FileDialog                  dlg=null;

dlg=new FileDialog(owner,"Select File to Load",FileDialog.LOAD);
dlg.setFile(null);
dlg.setVisible(true);

is there any way to get that dialog centered?

A: 

Appears that this may still be a bug.... see last line of this (though its dated 2003)

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4333836

I threw this together

        FileDialog fd = new FileDialog(f, title, FileDialog.LOAD);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    int w = fd.getSize().width;
    int h = fd.getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;

    System.out.println("Dialog location: " + fd.getLocation().toString());
    fd.setLocation(x, y); 
    System.out.println("Dialog location: " + fd.getLocation().toString());
    fd.setVisible(true);

And my output was:

Dialog location: java.awt.Point[x=0,y=0]

Dialog location: java.awt.Point[x=840,y=525]

But the screen was still in the top left corner

Holograham
This is what I have been seeing.
Software Monkey
This is not the correct bug link, since the reporter put the setLocationRealativeTo after setVisible, so, it was not considered a bug.Maybe another link points to the correct bug, with the setVisible as last action.. I'm looking for..
Tom Brito
At setVisible(), the calling thread is blocked until the dialog is dismissed; it seems that setVisible() somehow repositions the dialog.
Software Monkey