1

I'm having trouble trying to get a linebreak included in a Stringbuilder to appear in a JLabel.

I've found a couple of similar problems solved here, e.g. [here] Problems to linebreak with an int in JLabel and [here] How do I append a newline character for all lines except the last one? , along with a few others but none of them seem to work for me.

When printing to System.out, it works fine and I get a new line each time but when passed to a JLabel, everything appears as one line.

Here's the current version of the code, which has attempted to append a newline character, as well as including one in the main declaration. Neither has any effect when passed to the JLabel:

public void layoutCenter() {
    JPanel central = new JPanel();
    add(central, BorderLayout.CENTER);

    JTabbedPane tabs = new JTabbedPane();
    this.add(tabs);

    // memory tab
    StringBuilder mList = new StringBuilder();
    memLocList = new Memory[MEM_LOCATIONS]; //Memory is a separate class
    for (int i = 0; i < memLocList.length; i++) {
        mList.append("\n");
        memLocList[i] = null;
        mList.append("Memory location: " + i + " " + memLocList[i] + "\n");
    }
    System.out.println(mList.toString());

    JComponent memTab = makeTextPanel(mList.toString());
    tabs.addTab("Memory", memTab);

}

protected JComponent makeTextPanel(String text) {
    JPanel panel = new JPanel(false);
    JLabel filler = new JLabel(text);
    panel.add(filler);
    return panel;
}

I've also tried using System.getProperty(line.separator) with similar results and can't think of anything else to try so thought I'd appeal for help here.

Thanks, Robert.

-EDIT-

Thanks to mKorbel, changing the JLabel to a JTextPane solved it. The two lines in question are:

JTextPane filler = new JTextPane();
    filler.setText(text);

Thanks again to everyone.

Community
  • 1
  • 1
Robert
  • 5,278
  • 43
  • 65
  • 115

5 Answers5

2

You're going to have to use <html> and <br> to get line breaks in a JLabel Swing component.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
2

If you absolutely must use JLabel, then I suggest using one for each line.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
2

You can make a JLabel have mulitple lines by wrapping the text in HTML tags and using br tags to add a new line.

If you news auto wrapping I suggest using a JTexrArea. You can make it uneditable and style it so it looks like a label.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
2

JLabel isn't designated to held multilines document, there are two choices (by accepting newline or tab by default)

  • if document could not be decorated or styled somehow then to use JTextArea

  • in the case document could be decorated or styled somehow then to use JEditorPane or JTextPane

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thank you mKorbel, I changed it to JTextpane and it now formats correctly. Much appreciated. – Robert Jun 26 '12 at 13:27
  • As an aside, although this works very well, it takes a while to start up. Is there a more efficient way of doing this? (without the obvious, "don't use Java.") – Robert Jun 26 '12 at 19:33
  • the same start_up became from all programing languages, plus minus citibus, – mKorbel Jun 26 '12 at 19:48
2

You can look at this tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/html.html

One of the example is using html to make it two lines for a JButton text. It should be very similar.

evanwong
  • 5,054
  • 3
  • 31
  • 44