Note that there are some explanatory texts on larger screens.

plurals
  1. POIncluding linebreak in StringBuilder
    primarykey
    data
    text
    <p>I'm having trouble trying to get a linebreak included in a Stringbuilder to appear in a JLabel.</p> <p>I've found a couple of similar problems solved here, e.g. [here] <a href="https://stackoverflow.com/questions/8325337/problems-to-linebreak-with-an-int-in-jlabel">Problems to linebreak with an int in JLabel</a> and [here] <a href="https://stackoverflow.com/questions/448320/how-do-i-append-a-newline-character-for-all-lines-except-the-last-one">How do I append a newline character for all lines except the last one?</a> , along with a few others but none of them seem to work for me.</p> <p>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.</p> <p>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:</p> <pre><code>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 &lt; 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; } </code></pre> <p>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.</p> <p>Thanks, Robert.</p> <p>-EDIT-</p> <p>Thanks to mKorbel, changing the JLabel to a JTextPane solved it. The two lines in question are:</p> <pre><code>JTextPane filler = new JTextPane(); filler.setText(text); </code></pre> <p>Thanks again to everyone.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload