Reputation: 727
How can I display more than one line using JOptionPane.showMessageDialog()
. For example, I'm posting an example below. Now, in the example stated below the values a1,b1,c1
have to be displayed one by one using the JOptionPane.showMessageDialog()
Is there any way to show all the values in one window? because in the example stated below three windows will come one after one.
class demo()
{
public static void main(String args[])
{
String a=JOptionPane.showInputDialog(null,"Enter a number");
int a1=Integer.parseInt(a);
String b=JOptionPane.showInputDialog(null,"Enter a number");
int b1=Integer.parseInt(b);
int c=a1+b1;
JOptionPane.showMessageDialog(null,a1);
JOptionPane.showMessageDialog(null,b1);
JOptionPane.showmessageDialog(null,c1);
}
}
Upvotes: 4
Views: 26714
Reputation: 11
Why not try this:
JOptionPane.showMessageDialog(null, a1 + "\n" + b1 + "\n" + c1);
Which will print all in three different lines in a dialog.
Upvotes: 1
Reputation: 5996
If you want to put every value in a new line you don't need to use a JLabel
with HTML or a JTextArea
, you can simply use \n
in the String
:
JOptionPane.showMessageDialog(null, "line1\nline2");
Of course you can simply concatenate your values by adding them with a String
:
a1 + "\n" + a2
Upvotes: 6
Reputation: 1074
Why can't you just take all that in a string and use only one JOptionPane.
int c=a1+b1;
String s = "a1: "+a1+" b1: "+b1+"c: "+c;
JOptionPane.showMessageDialog(null,s);
instead of 3 JOptionPane.
Upvotes: 4
Reputation: 285405
Some ways to solve this:
setEditable(false)
on it.Upvotes: 5