0

I'm creating an About dialog for my program and have run into an issue. Whenever I launch the dialog, the title text sets properly, but the text inside the dialog says "JOptionPane Message". Before it wouldn't even show the label I added to the dialog, but that fixed itself (I'm not entirely sure how) but now it displays both the added text and the "JOptionPane Message".

Code for the dialog:

    about.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JOptionPane optionPane = new JOptionPane();
            JDialog dialog = optionPane.createDialog("About");
            JLabel aboutText = new JLabel("Text goes here", JLabel.CENTER);
            dialog.add(aboutText, BorderLayout.NORTH);
            dialog.setVisible(true);
        }
    });

So the text works now, which is nice. But how do I get rid of the part that says "JOptionPane Message"?

Thomas Ramage
  • 73
  • 1
  • 9
  • 2
    The question is, what's wrong with `JOptionPane.showMessageDialog(this, aboutText, "About", JOptionPane.INFORMATION_MESSAGE);` ? – MadProgrammer Dec 09 '13 at 01:04
  • The problem with that is I'd like the end result to be a logo centered on the dialog, with the about text displayed underneath of it. Is there a way I can do that with showMessageDialog()? I searched a bit but couldn't find anything – Thomas Ramage Dec 09 '13 at 01:11
  • Yes. You can use HTML to format the `String` or use the `JLabel` as the message object to the option pane... – MadProgrammer Dec 09 '13 at 01:12
  • So I'm assuming the proper way to do this would be `JLabel labelName = new JLabel("Text here", imgIcon, JLabel.CENTER);`? – Thomas Ramage Dec 09 '13 at 01:15

3 Answers3

3

I think people forget just how powerful JOptionPane can be...

If you supply a String as the message Object, JOptionPane will create a JLabel to display it with, if you supply some kind of Component, JOptionPane will simply use it instead, for example...

enter image description here

JLabel label = new JLabel("Bring it on");
label.setIcon(new ImageIcon("logo.png"));
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.BOTTOM);
label.setHorizontalTextPosition(JLabel.CENTER);
JOptionPane.showMessageDialog(null, label, "About", JOptionPane.PLAIN_MESSAGE);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • This is exactly what I needed, thank you! Setting @tbodt's response as the solution, as it's more relevant to the original question. I didn't know about `setIcon()` and if I'm not mistaken you can only use one label in `showMessageDialog()` which is why I went for JDialog. Thanks again! – Thomas Ramage Dec 09 '13 at 01:23
  • 1
    @ThomasRamage See my Answer for why this was occuring, Also you can still give upvotes as well as accepting an answer to show that it was still useful. – Java Devil Dec 09 '13 at 01:29
  • 1
    @ThomasRamage That would depend on you needs. You could just as easily pass `JOptionPane` a `JPanel` with other components added to it, for example... – MadProgrammer Dec 09 '13 at 01:30
  • Unfortunately I can't upvote until I've reached 15 reputation. Is it poor etiquette to come back once I've obtained enough reputation to give upvote? I'm a bit new to SO – Thomas Ramage Dec 09 '13 at 01:31
  • 1
    @ThomasRamage Welcome and Apologies, I forgot about the requirement to upvote. No It would not be poor etiquette. I Suggest you take the time to read through the [Help Center](http://stackoverflow.com/help) as to was is generally accepted/not accepted. – Java Devil Dec 09 '13 at 01:38
2

Here's what you do to procure the about dialog. Plus, it's only one line of code!

JOptionPane.showMessageDialog(this, "Text goes here", "About", JOptionPane.INFORMATION_MESSAGE);

I assume that this code is inside a subclass of JFrame. If not, replace this with any component inside the window where you want to dialog to appear, or just null for the center of the screen.

You may also want to change "Text goes here" to your own custom message text.

To change the type of message, change JOptionPane.INFORMATION_MESSAGE to one of the following:

  • JOptionPane.ERROR_MESSAGE
  • JOptionPane.WARNING_MESSAGE
  • JOptionPane.QUESTION_MESSAGE
  • JOptionPane.PLAIN_MESSAGE
tbodt
  • 16,609
  • 6
  • 58
  • 83
1

This occurs because you are using the default constructor new JOptionPane(). From the docs:

Creates a JOptionPane with a test message.

This test message is "JOptionPane Message"

If you use the constructor new JOptionPane("A Message") then the message will become "A Message" where this constructor is taking an Object so using a Label your code would become

JLabel aboutText = new JLabel("Text goes here", JLabel.CENTER);
JOptionPane o = new JOptionPane(aboutText);
JDialog dialog = optionPane.createDialog("About");
dialog.setVisible(true);

You can also use the constructor new JOptionPane(null) to have no object or default message so you can add them later if you like.

Java Devil
  • 10,629
  • 7
  • 33
  • 48