JOptionPane: add horizontal line

I have a JOptionPane and would like to add a horizontal line... I am using hyphens but it doesn't look good... e.g.:

JOptionPane.showMessageDialog(frame,"The correct answer is:\n" +

"A = 2\n" +

"B = 2\n" +

"--\n" +

"C= 4");

Could I replace the hyphens with a horizontal line... maybe like javax.swing.JSeparator?

Thanks!

[496 byte] By [SFLa] at [2007-11-27 8:40:32]
# 1
You can add any component you want to a JOptionPane. It doesn't have to be a String of text. So you could add a JTextArea with formatted text, or you could add a panel with multiple objects.
camickra at 2007-7-12 20:39:02 > top of Java-index,Desktop,Core GUI APIs...
# 2

....but how exactly?

JOptionPane.showMessageDialog(frame, "The correct answer is:\n" +

"A = 2\n" +

"B = 2\n" +

add(new JSeparator(SwingConstants.HORIZONTAL) + // doesn't work

"C= 4");

How can I construct this?

SFLa at 2007-7-12 20:39:02 > top of Java-index,Desktop,Core GUI APIs...
# 3
How do you build a normal panel with a bunch of labels and other components?
camickra at 2007-7-12 20:39:02 > top of Java-index,Desktop,Core GUI APIs...
# 4
Oh, so I have to put 3 JLabels into the JOptionPane: one with the first 2 variables A and B, one for the JSeparator and one for the result C...? Isn't there an easier way?
SFLa at 2007-7-12 20:39:02 > top of Java-index,Desktop,Core GUI APIs...
# 5

[nobr]> Oh, so I have to put 3 JLabels into the JOptionPane:

> one with the first 2 variables A and B, one for the

> JSeparator and one for the result C...? Isn't there

> an easier way?

JOptionPane.showXXXXDialog() takes only one Object as "message" parameter.

You need to put your 3 JLabels (or whatever) into a e.g. JPanel, then pass only this JPanel to showXXXXDialog(). Once you see things like this, it's not difficult at all.

Otherwise, you can use just a JLabel and format your text with HTML, using the HR tag for the horizontal line:

new JLabel("<html>A = 2<br>B = 2<br><hr><br>C = 4</html>");

Not sure if you love how it looks like, though.[/nobr]

java_knighta at 2007-7-12 20:39:02 > top of Java-index,Desktop,Core GUI APIs...
# 6

I changed it now to the following:

JOptionPane.showMessageDialog(frame, "The correct answer is:\n" +

"A = 2\n" +

"B = 2\n" +

"<html><body><hr width=150 size=1></body></html>" +

"C= 4");

It works, but I don't know if it's recommended to solve this problem this way...?

SFLa at 2007-7-12 20:39:02 > top of Java-index,Desktop,Core GUI APIs...
# 7

> It works, but I don't know if it's recommended to solve this problem this way...?

For now all that matters is that it works. Within the next few days or weeks depending on how much you experiment, you'll probably find that there is a much prettier or more efficient way to do this and you'll have to re-code. But then again, that is how we all learn.

ICE

icewalker2ga at 2007-7-12 20:39:02 > top of Java-index,Desktop,Core GUI APIs...