10

I want to make JOptionPane.showMessageDialog message appear

  • Any place in the screen.
  • Relative to JFrame. (not at the centre of the JFrame)

For example this will display the message at the centre of the JFrame provided as argument thisFrame

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

And this will display the message at the centre of the screen irrelative to any JFrame.

JOptionPane.showMessageDialog(null, "Your message.");
  • what I want is to set the location of the message any place I want

  • what I want is to set the location of the message relative to the JFrame (not at the centre of the JFrame)

How?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Saleh Feek
  • 2,048
  • 8
  • 34
  • 56
  • Is my question illegal/invalid in terms of GUI doctrine?! @AndrewThompson – Saleh Feek Dec 07 '12 at 09:28
  • 3
    Note that `JOptionPane` can use ***any*** `Component` as parent. That means, it can be relatice to the forame, any component inside it, any component any in/any floating window (tool-bar, `JWindow`, `JDialog`) that is on-screen) or of course, `null` (center of screen). – Andrew Thompson Dec 07 '12 at 09:29
  • 1
    This [thread](http://stackoverflow.com/q/9807890/1057230) might be of some interest to you. – nIcE cOw Dec 07 '12 at 09:47

3 Answers3

9

What you need is

    final JOptionPane pane = new JOptionPane("Hello");
    final JDialog d = pane.createDialog((JFrame)null, "Title");
    d.setLocation(10,10);
    d.setVisible(true);
Neil Wightman
  • 1,103
  • 2
  • 9
  • 29
5
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);
    }
}
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
-2

Try this

JOptionPane pane = new JOptionPane(arguments);
pane.setBounds(x, y,width, height);   
pane.setVisible(true);
vels4j
  • 11,208
  • 5
  • 38
  • 63