How to set the position of JOptionPane

I create this JOptionPane

 JOptionPane.showMessageDialog(this, "File was saved", "Save", JOptionPane.INFORMATION_MESSAGE); 

but my JFrame is big so it scrolls. When I call this command, a window is created in the lower right corner, and I can only see the title. How can I reposition this JOptionPane ?

+6
source share
2 answers

According to api 1.6:

first parameter of parentComponent :

Defines the component that should be the parent of this dialog box. It is used in two ways: the frame that contains it is used as the parent of the frame for the dialog box, and its screen coordinates are used when placing the dialog box. In general, a dialog box is located just below the component. This parameter may be zero, in which case the default frame is used as the parent element, and the dialog will be centered on the screen (depending on L & F).

So there is no parameter to set the position of the JOptionPane, but you could at least pass null as the first parameter to make sure the JOptionPane is clearly visible and centered.

+5
source

You can create a JDialog from a JOptionPane (see the JOptionPane API to see how to do this) and then display it anywhere you want with any JDialog. By the way, maybe you want to make your JFrame smaller using JTabbedPanes or CardLayout so that you don't have this problem.

+3
source

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


All Articles