2

I have a string that I want to display in a JOptionPane.

String info = "Name:" + _name + "\n" +
              "Phone:" + _phone;

I tried to add \t but it didn't work.

I tried also to

int choose = JOptionPane.showConfirmDialog(this,new JTextArea(info),"XXX",0);

But it doesn't look good.

Are there another ways to do that? (If you know about a solution where I can use something like \t it will be very usefull for me)

* In this specific example I can manually align it, but I'm looking for a general solution.

Maroun
  • 94,125
  • 30
  • 188
  • 241

2 Answers2

5

HTML formatting could help:

String info = "Name:" + _name + "<br>" +
              "Phone:" + _phone + "<br>";

int choose = 
      JOptionPane.showConfirmDialog(this, "<html>" + info + "</html>", "XXX", 0);

Another option is to use a JTable in the JOptionPane dialog as shown in this solution.

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • I tried to do so, still doesn't work. (I have more fields, not only `_name` and `_phone`. I want to align it so `_name`, `_phone` and another fields will start from the same column). – Maroun Feb 10 '13 at 12:49
  • 1
    You could use a `HTML` table or use `CSS` to do alignment – Reimeus Feb 10 '13 at 12:51
1

I had something like that and it worked:

String st =        "<table border = \"0\">" +
                   "<tr><td>VALUE1:  </td><td>" + _value2 + "</td></tr>"+
                   "<tr><td>VALUE2:  </td><td>" + _value4 + "</td></tr>" +
                   //.....
                   "</table>";
Maroun
  • 94,125
  • 30
  • 188
  • 241