Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9123965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:27:57+00:00 2026-06-17T06:27:57+00:00

My Swing application has to show a modal dialog to the user. Sorry for

  • 0

My Swing application has to show a modal dialog to the user. Sorry for not posting SSCCE.

topContainer might be JFrame or JApplet.

private class NewGameDialog extends JDialog {
     public NewGameDialog () {
         super(SwingUtilities.windowForComponent(topContainer), "NEW GAME", ModalityType.APPLICATION_MODAL);

         //add components here

         getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

         //TODO:
         setSize(new Dimension(250, 200));
         setLocation(650, 300);
     }
}

I start the dialog like this on network event

SwingUtilities.invokeLater(new Runnable() {
     @Override
     public void run() {
         NewGameDialog dialog = new NewGameDialog();
         dialog.setVisible(true);
     }
});

The problem is to set optimal location for my dialog.

1) If it is set as absolute value, and I move the app frame to the second screen, then dialog is shown on the first screen which is weird.

2) If it is set a relative value to JFrame, it might appear that user moved the app frame outside of the screen and the dialog being relatively located would not be visible to the user. And because it is modal, the game would be stuck.

What is the best solution considering two above mentioned issues?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:27:59+00:00Added an answer on June 17, 2026 at 6:27 am

    This reminded me of a very favourite post of mine, using Window.setLocationByPlatform(true), on StackOverflow.

    How to best position Swing GUIs

    EDIT 1 :

    You can add a FocusListener to your JDialog and on focusGained(...) method, you can use setLocationRelativeTo(null) for both the JFrame and the JDialog, so that they both come to the center of the screen no matter where they are before.

    import java.awt.*;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Gagandeep Bali
     * Date: 1/14/13
     * Time: 7:34 PM
     * To change this template use File | Settings | File Templates.
     */
    public class FrameFocus
    {
        private JFrame mainwindow;
        private CustomDialog customDialog;
    
        private void displayGUI()
        {
            mainwindow = new JFrame("Frame Focus Window Example");
            customDialog = new CustomDialog(mainwindow, "Modal Dialog", true);
            mainwindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            JButton mainButton = new JButton(
                    "Click me to open a MODAL Dialog");
            mainButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!customDialog.isShowing())
                        customDialog.setVisible(true);
                }
            });
            contentPane.add(mainButton);
            mainwindow.setContentPane(contentPane);
            mainwindow.pack();
            mainwindow.setLocationByPlatform(true);
            mainwindow.setVisible(true);
        }
    
        public static void main(String... args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new FrameFocus().displayGUI();
                }
            });
        }
    }
    
    
    class CustomDialog extends JDialog
    {
        private JFrame mainWindow;
        public CustomDialog(JFrame owner, String title, boolean modal)
        {
            super(owner, title, modal);
            mainWindow = owner;
            JPanel contentPane = new JPanel();
            JLabel dialogLabel = new JLabel(
                    "I am a Label on JDialog.", JLabel.CENTER);
            contentPane.add(dialogLabel);
            setContentPane(contentPane);
            pack();
    
            addFocusListener(new FocusListener() {
                @Override
                public void focusGained(FocusEvent e) {
                    mainWindow.setLocationRelativeTo(null);
                    setLocationRelativeTo(null);
                }
    
                @Override
                public void focusLost(FocusEvent e) {
                    /*
                     * Nothing written for this part yet
                     */
                }
            });
        }
    }
    

    EDIT 2 :

    I searched a bit here and there, and it turns out, in my opinion, that actually on which Monitor Screen your application comes at the first instance, will determine it’s GraphicsConfiguration. Though as I roamed through the API, there is only a getter method for the said GraphicsConfiguration thingy and no setter methods for the same (Still You can specify one through the constructor of any top level Window i.e. JFrame(…)/JDialog(…)).

    Now you can occupy your head with this code, which can be used to determine the appropriate location, that you want to set, again, you might have to use focusGain() method in my opinion, to satisfy condition 2 of your question. Have a look at the code attached, though no need to create a new JFrame/JDialog, just watch how to get coordinates for the screen (that you can add in the focusGain() method to determine the location of the whole Application.)

    GraphicsEnvironment ge = GraphicsEnvironment.
        getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    for (int j = 0; j < gs.length; j++) {
        GraphicsDevice gd = gs[j];
        GraphicsConfiguration[] gc =
                gd.getConfigurations();
        for (int i=0; i < gc.length; i++) {
            JFrame f = new
            JFrame(gs[j].getDefaultConfiguration());
            Canvas c = new Canvas(gc[i]);
            Rectangle gcBounds = gc[i].getBounds();
            int xoffs = gcBounds.x;
            int yoffs = gcBounds.y;
            f.getContentPane().add(c);
            f.setLocation((i*50)+xoffs, (i*60)+yoffs);
            f.show();
        }
    }
    

    EDIT 3 :

    Try to change this :

    int x = loc.getX() + (mainWindow.getWidth() - getWidth()) / 2;
    int y = loc.getY() + (mainWindow.getHeight() - getHeight()) / 2;
    setLocation(x, y);
    

    to just :

    setLocationRelativeTo(mainWindow);
    

    To test the above thingy, I used my FrameFocus Class as is, though I had added your changes to my CustomDialog method, as shown in this modified CustomDialog Class.

    class CustomDialog extends JDialog
    {
        private JFrame mainWindow;
        public CustomDialog(JFrame owner, String title, boolean modal)
        {
            super(owner, title, modal);
            mainWindow = owner;
            JPanel contentPane = new JPanel();
            JLabel dialogLabel = new JLabel(
                    "I am a Label on JDialog.", JLabel.CENTER);
            contentPane.add(dialogLabel);
            setContentPane(contentPane);
            pack();
    
            addFocusListener(new FocusListener() {
                @Override
                public void focusGained(FocusEvent e) {
                    //mainWindow.setLocationRelativeTo(null);
                    //setLocationRelativeTo(null);
                    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                    GraphicsDevice[] gs = ge.getScreenDevices();
                    for (int j = 0; j < gs.length; j++) {
                        GraphicsDevice gd = gs[j];
                        GraphicsConfiguration[] gc = gd.getConfigurations();
                        for (int i=0; i < gc.length; i++) {
                            Rectangle gcBounds = gc[i].getBounds();
    
                            Point loc = mainWindow.getLocationOnScreen();
                            if (gcBounds.contains(loc)) {
                                System.out.println("at " + j + " screen");
    
                                int x = gcBounds.x + (gcBounds.width - mainWindow.getWidth()) / 2;
                                int y = gcBounds.y + (gcBounds.height - mainWindow.getHeight()) / 2;
                                mainWindow.setLocation(x, y);
    
                                //x = (int) (loc.getX() + (mainWindow.getWidth() - CustomDialog.this.getWidth()) / 2);
                                //y = (int) (loc.getY() + (mainWindow.getHeight() - CustomDialog.this.getHeight()) / 2);
                                //CustomDialog.this.setLocation(x, y);
                                CustomDialog.this.setLocationRelativeTo(mainWindow);
    
                                break;
                            }
                        }
                    }
                }
    
                @Override
                public void focusLost(FocusEvent e) {
                    /*
                     * Nothing written for this part yet
                     */
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Hibernate J2SE Swing application and I would like to show user
I have a Swing Application where a user has to Input Some information. I
So I have a strange problem, I have a java swing application that has
I have developed a java swing client-server application. The server has many services like
I have a Swing application that connect directly to MySql Database for user authentication.
In my Swing application, the user must insert numbers and values, before switching to
How would I go about getting the Windows user credentials from a Swing application?
I want to show a MessageBox in a VB.NET application that has special markup
I have a Java Swing application, that has many JTextFields and a datamodel. When
I have a swing application and on the JFrame's menu I want to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.