15

I need a text pop up like the one you get with JOptionPane.showInputDialog(null, "Text"); Just with multiple lines, like...

I'm new to java.
I have no background in programming.
I could use some help

How would I do this?

David Kroukamp
  • 34,930
  • 13
  • 72
  • 130
jjd712
  • 151
  • 1
  • 1
  • 4

2 Answers2

18

You could use '\n' like so:

JOptionPane.showMessageDialog(null, "Hello\nworld");
David Kroukamp
  • 34,930
  • 13
  • 72
  • 130
  • 3
    -1 He wants to get an input text from the user (with the input dialog looking like a text area), not output multiple lines to the user. – trinity420 Apr 20 '17 at 19:31
16

There's probably a dozen other ways to do this, but the simplest I can think of is

JOptionPane.showMessageDialog(null, "<html>I'm new to java.<br>I have no background in programming.<br>I could use some help Thanks!</html>");

Another approach to demonstrate the power of the JOptionPane

JTextArea msg = new JTextArea("This is a really silly example of what can be achieved with a JOptionPane, but this is going to excese for what you have asked for");
msg.setLineWrap(true);
msg.setWrapStyleWord(true);

JScrollPane scrollPane = new JScrollPane(msg);

JOptionPane.showMessageDialog(null, scrollPane);
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
  • 3
    @DavidKroukamp Love HTML in Java, just wish it had better CSS support – MadProgrammer Aug 25 '12 at 00:43
  • Have you had peek at [Skinning JavaFX Applications with CSS](http://docs.oracle.com/javafx/2/css_tutorial/jfxpub-css_tutorial.htm) and you can [style layouts using CSS in JavaFX](http://docs.oracle.com/javafx/2/layout/style_css.htm) – David Kroukamp Aug 25 '12 at 00:45
  • IS there a difference between this and using /n? or is it personal preference? ...Disregard this post... – jjd712 Aug 25 '12 at 00:50
  • @jjd712 Given the requirements, David's solutions is simpler. If you wanted to display a more complex message (such as images or tabular data), then this would be more useful. I use a similar approach when I want to display complex tooltips. Check update for a even more complex, but viable, solution – MadProgrammer Aug 25 '12 at 01:07
  • *"wish it had better CSS support"* .. `I'm new to java.
    ` Not really relevant to this question (where the OP seems to know where they want line breaks) but at least the J2SE supports the CSS `width` attribute if added to the `body` element. That can help us avoid having to figure out were to put line breaks in multiline text. :)
    – Andrew Thompson Aug 25 '12 at 01:21