How to center java.awt.FileDialog on screen

I could never understand this; ordinary suspects do not 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 this dialog with the center?

The key point is that in setVisible (), the calling thread is blocked until the dialog is rejected; and any positioning before that seems to be ignored.

+3
source share
4 answers

The solution below works for SWT, maybe it can also do the trick for AWT ...

, - , FileDialog. :

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

public class CenteredFileDialog extends Dialog {

    protected Shell shell;
    public FileDialog dialog;

    private int width = 560; // WinXP default
    private int height = 420;

    public CenteredFileDialog(Shell parent, int style) {
        super(parent, style);
        shell = new Shell(getParent(), SWT.APPLICATION_MODAL);
        dialog = new FileDialog(shell, style);
    }

    public Object open() {
        shell.setSize(width, height);

        Rectangle parentBounds = getParent().getBounds();

        shell.setLocation(
          parentBounds.x + (parentBounds.width - width) / 2,
          parentBounds.y + (parentBounds.height - height) / 2);

        Object result = dialog.open();
        shell.dispose();
        return result;
    }
}

:

CenteredFileDialog saveDialog = new CenteredFileDialog(getShell(), SWT.SAVE);
saveDialog.dialog.setFilterExtensions(new String[] { "*.txt" });
saveDialog.dialog.setFilterNames(new String[] { "Text (*.txt)" });
...
String f = (String)saveDialog.open();
if ( f != null ) {
    name = f;
    recentPath = saveDialog.dialog.getFilterPath();
} 

Windows ( MacOS , Linux ) - ( , ), "" . , .

, , , , , .

+7

, .... . ( 2003)

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

        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);

:

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

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

+1

: dlg.setLocationRelativeTo(null);

0

Java 7, Eclipse 4.4.1 Ubuntu 14.04 AWT FileDialog.

, Apple awt.FileDialog Swing JFileChooser .

The trick is to provide your instance with FileDialoga sizebefore installing it location.

Use boundsfor contentPaneyour main application frameto calculate the distance from the left corner Point(minX, minY) from FileDialogthe center contentPane Point.

Then set locationyours FileDialogto this calculated Point, et voilรก ... centered.

    final FileDialog fileDialog = new FileDialog(applicationFrame, 
            "Choose a file", FileDialog.LOAD);

    /* Lots of code to be able to center an awt.FileDialog on screen... */
    Rectangle rect = applicationFrame.getContentPane().getBounds();

    /* 
     * Making sure FileDialog has a size before setVisible, otherwise
     * left corner distance from contentPane center cannot be found.
     */
    fileDialog.pack();
    fileDialog.setSize(800, 600);
    fileDialog.validate();

    double width = fileDialog.getBounds().getWidth();
    double height = fileDialog.getBounds().getHeight();

    double x = rect.getCenterX() - (width / 2);
    double y = rect.getCenterY() - (height/ 2);

    /* Could be new Point(x, y) */
    Point leftCorner = new Point();
    leftCorner.setLocation(x, y);

    fileDialog.setLocation(leftCorner);

    fileDialog.setVisible(true);
0
source

Source: https://habr.com/ru/post/1737446/


All Articles