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?

3
  • Is my question illegal/invalid in terms of GUI doctrine?! @AndrewThompson
    – Saleh Feek
    Commented Dec 7, 2012 at 9: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). Commented Dec 7, 2012 at 9:29
  • 1
    This thread might be of some interest to you.
    – nIcE cOw
    Commented Dec 7, 2012 at 9:47

3 Answers 3

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);
1
  • 5
    You can use d.setLocationRelativeTo(someparent); to position it relative to any UI component. Commented Dec 7, 2012 at 9:37
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);
    }
}
3
  • Thats not the answer to the question.
    – UNIQUEorn
    Commented Aug 9, 2018 at 8:02
  • 1
    @UNIQUEorn I think it is. Commented Aug 9, 2018 at 8:13
  • 1
    Ok this is actually a way to do it. But you do not need to build a custom solution including Yes/No-Buttons. Just create the JOptionPane and place it onto a JDialog instance.
    – UNIQUEorn
    Commented Aug 10, 2018 at 9:13
-2

Try this

JOptionPane pane = new JOptionPane(arguments);
pane.setBounds(x, y,width, height);   
pane.setVisible(true);
4
  • 3
    "Try this" Did you try it? Nothing appears when run in my JRE. Commented Dec 7, 2012 at 9:32
  • JOptionPane is a JComponent not a dialog or a window. Commented Dec 7, 2012 at 9:38
  • @AndrewThompson Sorry thompson, I did not try. Only checked those 3 lines.
    – vels4j
    Commented Dec 7, 2012 at 10:01
  • @Neil Wightman i knew but it has those methods. May be my answer is worng.
    – vels4j
    Commented Dec 7, 2012 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.