1

I am using the answer to this question in order to word-wrap my JLabel's text and the code I am using to do that is this:

label.setText("<html style=\"vertical-align:middle;\">"+label.getText()+"</html>");

But the label displays the text wrong. For example if the label's text was "the big bad wolf" what I see is this:

<html style="vertical-align:middle;">the big bad wolf</html>

Why is the label not accepting my style?
UPDATE: Here is an example of what I am trying to do: enter image description here

Lae
  • 588
  • 1
  • 7
  • 26

1 Answers1

1

Try

private String warpWithHtmlTags(String...strings) {

    StringBuilder sb = new StringBuilder("<html>");
    sb.append("<div style = \"text-align: center;\">");

    for(String s : strings) {

        sb.append("<p>")
          .append(s)
          .append("</p>");
    }

    sb.append("</div></html>");
    return sb.toString();
}

Test with :

  String text = warpWithHtmlTags("line one", "longer line two", "even longer line three");
  JLabel label = new JLabel(text);
c0der
  • 15,550
  • 6
  • 26
  • 53
  • This worked but I I don't understand how the `div` is accepting the given style when it is not contained within quotes. Should it not be like this: `
    `
    – Lae Jul 01 '17 at 14:23
  • 1
    Thanks for the comment. Apparently it works both ways. I updated my answer. – c0der Jul 01 '17 at 14:32