Hello everyone!
Just as the title suggests, i would like to know if it's possible to set the position of a dialog box.
The default behaviour, is to appear in the center of the screen
I use it this way :

    public static int confirmationBox(String infoMessage, String location)
    {
        int response = JOptionPane.showConfirmDialog(null, infoMessage, "Confirmation : " + location, JOptionPane.INFORMATION_MESSAGE);
        return response;
    }

Thanks in advance!

Recommended Answers

All 4 Replies

  • change null from int response = JOptionPane.showConfirmDialog(null ... to real parent, any (J)Component that returns true from isDisplayable(), means that is already visible on the screen, then JOptionPane will be centered to this Point

  • JOptionPane is only the JDialog, then there is possible to moving with locationOnScreen,

  • I'd suggest to use coordinates from parent v.s. JOptionPane, instead of (dirty and can be fragile) hardcoding location on the screen

Create a new JOptionPane, let it create a JDialog that contains it, position the JDialog, make it visible... eg something like

      JOptionPane pane = new JOptionPane("Click one:", 
           JOptionPane.QUESTION_MESSAGE,  JOptionPane.YES_NO_CANCEL_OPTION);
      JDialog d = pane.createDialog(null); // see mKorbel's post for better ways to do this
      d.setLocation(400,400);              // see mKorbel's post for better ways to do this
      d.setVisible(true);
      // modal dialog - automatically waits here until dialog closed
      System.out.println(""Dialog was closed with button " + pane.getValue());
      d.dispose();

First suggestion worked as expected. The dialog appears in the center of the parent.
For the second i did the following :

    public int confirmationBox(String infoMessage, String location)
    {
        JOptionPane a = new JOptionPane();
        a.setLocation(100,100);

        int response = a.showConfirmDialog(null, infoMessage, "Confirmation : " + location, JOptionPane.INFORMATION_MESSAGE);
        return response;
    }

but it didn't work. Neither with null as parent nor with the actual parent window.

Could you please elaborate on this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.