Question

Here is my code :

JLabel JL_Output = new JLabel(Reponse);
JL_Output.setBorder(BorderFactory.createTitledBorder("Output: "));
JL_Output.setPreferredSize(new Dimension(450, 175));
JL_Output.setBackground(Color.red);

So my probleme is that my Response String its too long to be viewed in one line. So I want to use the html with the /br nut I can't integrat these balise because I can't "enter" in the String :/

I would like to display in 4 lines, like this Response value :

Enter the angle offset
   Units   : [degrees]
   Range   : [-090<+090]
   Current : [+000]                 >>  ERROR
Was it helpful?

Solution

Assuming that Response is a string, you could do something like so:

String formattedResponse = Response.replaceAll("\\]", "\\]<br />");
JLabel JL_Output = new JLabel(formattedResponse);
...

That should replace all closing brackets with a closed bracket and a breakline statement, which should do what you need.

EDIT: As per your comment, you could use something like the code below to do what you need:

String formattedResponse = Response.replaceAll("\\s{4,}", "<br />");
JLabel JL_Output = new JLabel("<html>" + formattedResponse + "</html>");

The code above will match any piece of the Response string that is made up from 4 or more consecutive white space characters and replace them with a <br />.

OTHER TIPS

I understand from your description, that the text you receive is to long for one line and has no line breaks in it? So why don't you create a second String with line breaks in the places you want?

But maybe you could just use "JTextarea" an disable it, to make it not modifiable:

JTextArea textArea = new JTextArea(
  "This is an editable JTextArea. " +
  "A text area is a \"plain\" text component, " +
  "which means that although it can display text " +
  "in any font, all of the text is in the same font."
);
textArea.setFont(new Font("Serif", Font.ITALIC, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

Of course you can transform your string into HTML. See this article to find out how line breaks work: Multiline text in JLabel

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top