4

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);
    } 
}
mKorbel
  • 108,320
  • 17
  • 126
  • 296
Anurag Singh
  • 707
  • 6
  • 10
  • 26

4 Answers4

6

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
siegi
  • 4,769
  • 1
  • 28
  • 40
5

Some ways to solve this:

  • create a JLabel to hold your output and use HTML to allow the display of a multi-lined JLabel, then pass the JLabel as the second parameter of your JOptionPane.showMessageDialog.
  • Or you could create a JTextArea, append your result Strings + "\n" to the JTextArea, and then pass it into the JOptionPane.showMessageDialog method. If you do this, prevent others from editing your JTextArea by calling setEditable(false) on it.
Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
  • 2
    @user1334573: regarding your request, "can you please write the code for it." That's not how things are done here and not how you will learn. Rather, you will learn much more if *you* try to write the code for it using our suggestions. Then if you get stuck, show us your efforts. – Hovercraft Full Of Eels Apr 22 '12 at 04:02
  • For a basic implementation of the 1st suggestion, see [this answer](http://stackoverflow.com/a/7861833/418556). – Andrew Thompson Apr 22 '12 at 04:16
  • Ah yes, that's not so basic, but rather uses stylesheets, and is much slicker than hard-coding HTML breaks. In all a very nice solution. – Hovercraft Full Of Eels Apr 22 '12 at 04:17
  • I was reluctant to put that, after both agreeing with and up-voting your comment. OTOH - I had the technology.. (vague shrug) ;) – Andrew Thompson Apr 22 '12 at 04:19
  • @user1334573 : Here is another way how you can solve your query, by adding a `JPanel` to your `JOptionPane`, it's [another wonderful example](http://stackoverflow.com/a/10150308/1057230) by HovercraftFullOfEels :-) – nIcE cOw Apr 22 '12 at 04:19
  • +1 Yet another variation using a panel shown [here](http://stackoverflow.com/a/3002830/230513). – trashgod Apr 22 '12 at 09:29
4

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.

Nikhar
  • 1,054
  • 8
  • 23
1

Why not try this:

JOptionPane.showMessageDialog(null,  a1 + "\n" + b1 + "\n" + c1);

Which will print all in three different lines in a dialog.

fvrghl
  • 3,331
  • 4
  • 25
  • 34
Sun Pii
  • 11
  • 1