How to set the location of "JOptionPane.showMessageDialog"

I want to make a message to JOptionPane.showMessageDialog

  • Any place on the screen.
  • Regarding JFrame. (not in the center of the JFrame)

For example, this will display a message in the center of the JFrame provided as the argument to thisFrame

  JOptionPane.showMessageDialog(thisFrame, "Your message."); 

And this will display a message in the center of the screen that is not related to any JFrame.

 JOptionPane.showMessageDialog(null, "Your message."); 
  • I want to set the location of the message anywhere I want

  • I want to set the location of the message relative to the JFrame (not in the center of the JFrame)

How?

+7
source share
3 answers
 import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; public class CustomDialog extends JDialog { private JPanel myPanel = null; private JButton yesButton = null; private JButton noButton = null; public CustomDialog(JFrame frame, boolean modal, String myMessage) { super(frame, modal); myPanel = new JPanel(); getContentPane().add(myPanel); myPanel.add(new JLabel(myMessage)); yesButton = new JButton("Yes"); myPanel.add(yesButton); noButton = new JButton("No"); myPanel.add(noButton); pack(); //setLocationRelativeTo(frame); setLocation(200, 200); // <-- setVisible(true); } } 
+5
source

You need

  final JOptionPane pane = new JOptionPane("Hello"); final JDialog d = pane.createDialog((JFrame)null, "Title"); d.setLocation(10,10); d.setVisible(true); 
+8
source

try it

 JOptionPane pane = new JOptionPane(arguments); pane.setBounds(x, y,width, height); pane.setVisible(true); 
0
source

All Articles