-2

I have a JLabel that I would like to contain a text that can go over multiple lines, and resizes if the window changes shape.

I've looked this up and most people seem to recommend wrapping the label text in HTML. This however does not make new lines for me.

My label is located in a JPanel and I suspect that the problem may be that my panel has misconfigured its border, and so the label text just continues beyond the panel border.

here is how the label looks inside the status panel

Here are the settings of the panel:

private final JPanel statusPanel = new JPanel(new FlowLayout());
statusPanel.setBackground(Color.white);
        statusPanel.add(latestOrdreLabel);
        this.add(statusPanel, new GridBagConstraints(0, 0, 6, 1, 1.0, 1.0
                , GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));

Then the label is set up like this:

private final JLabel latestOrdreLabelResult = new JLabel();

String latestOrdreStatus = getBean().getLatestOrdreStatus(etelOrderInterface.getOrderId());

       latestOrdreLabelResult.setText("<html>"+latestOrdreStatus+"</html>");

        statusPanel.add(latestOrdreLabelResult);
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
Mimeer
  • 51
  • 7
  • what is the content of `latestOrdreStatus`? Does it include `
    `tags?
    – jhamon Sep 02 '19 at 15:01
  • Possible duplicate of [Multiline text in JLabel](https://stackoverflow.com/questions/685521/multiline-text-in-jlabel) – jhamon Sep 02 '19 at 15:01
  • 1
    See [this answer](https://stackoverflow.com/questions/7861724/is-there-a-word-wrap-property-for-jlabel/7861833#7861833) which, unlike the Q&A linked by @jhamon, does *not* rely on manually inserting `
    ` elements for each line. General tip: For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/).
    – Andrew Thompson Sep 02 '19 at 15:21
  • 2
    BTW - note that ***neither*** linked answer will automatically rewrap the text when the width of the label is made bigger or smaller. For something that does that, try a `JTextArea` with word wrap enabled, or a `JEditoPane`. – Andrew Thompson Sep 02 '19 at 15:25
  • @jhamon it doesn't contain br tags, as Ideally the jlabel would be changing where the line changes are depending on the size of window. And thanks for your suggestions Andrew, I'll look into them – Mimeer Sep 02 '19 at 17:00
  • 1
    Thanks @AndrewThompson While I previously had tried both option I hadn't set a width for the Jlabel, and hadn't enabled word wrap when I was looking at JTextarea. this is exactly what I wanted – Mimeer Sep 03 '19 at 07:47
  • Possible duplicate of [Is there a "word wrap" property for JLabel?](https://stackoverflow.com/questions/7861724/is-there-a-word-wrap-property-for-jlabel) – c0der Sep 04 '19 at 16:16

2 Answers2

0

In HTML, to down line your'e need to use <br/> instead of \n. You can to use in replace String method, for example: latestOrdreStatus.replace("\n", "<br/>");

Programmer
  • 531
  • 2
  • 10
-2

You just need to resize the label.

Wai Ha Lee
  • 7,664
  • 52
  • 54
  • 80