Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set position of JOptionPane

I'm creating this JOptionPane

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

but my JFrame is big so it is scrollable. When I call this command, a window is created in the bottom right corner and I can only see the header. How I can change the position of this JOptionPane?

like image 785
hudi Avatar asked Nov 15 '11 22:11

hudi


People also ask

How do you make JOptionPane always on top?

JOptionPane optionPane = new JOptionPane(); JDialog dialog = optionPane. createDialog("Title"); dialog. setAlwaysOnTop(alwaysOnTop); dialog. setVisible(true);

What are the 4 JOptionPane dialog boxes?

The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.

How do you make a new line in JOptionPane?

You have to use \n to break the string in different lines. Or you can: Another way to accomplish this task is to subclass the JOptionPane class and override the getMaxCharactersPerLineCount so that it returns the number of characters that you want to represent as the maximum for one line of text.


2 Answers

According to the api 1.6:

the first parameter is parentComponent:

Defines the Component that is to be the parent of this dialog box. It is used in two ways: the Frame that contains it is used as the Frame parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be null, in which case a default Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).

So there isn't no parameter to set the position of the JOptionPane, but you could at least pass null as first parameter to be sure your JOptionPane is well visible and centered.

like image 58
Renaud Dumont Avatar answered Sep 23 '22 02:09

Renaud Dumont


You could create a JDialog out of a JOptionPane (see the JOptionPane API to see how to do this), and then display it anywhere you'd like as you can with any JDialog. By the way, perhaps you want to make your JFrame smaller by using JTabbedPanes or CardLayout so you don't have this problem.

like image 34
Hovercraft Full Of Eels Avatar answered Sep 23 '22 02:09

Hovercraft Full Of Eels