0

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
Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
D3fman
  • 85
  • 2
  • 11

2 Answers2

1

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 />.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • Yes sure this would work only for this response but I have like 100 of response possible. Is it just possible to put a if there is more than 4 blank space ? – D3fman Mar 14 '14 at 13:23
  • Ok, I think I understand the logical, I will let you know if it worked. Thanks – D3fman Mar 14 '14 at 13:37
  • @D3fman: Ok. Just an FYI, I had a small typo in my edit (missed a set of quotation marks) so make sure you do not make my same mistake. – npinti Mar 14 '14 at 13:42
  • Last small question, how can you put in the begining of the string ? – D3fman Mar 14 '14 at 14:20
  • Thanks, yeah I forgot the "" -_- – D3fman Mar 17 '14 at 07:38
0

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

Community
  • 1
  • 1
  • The thing is how do you get in the reponse text ? because the reponse is something that is generate by a device and I get the response by doing a readSerialPort then I convert the reponse in bytes into String type. – D3fman Mar 14 '14 at 13:32
  • Yes but you can copy and modify the text. Like taking 40 characters, insert a line break, and take the next 40 characters and insert the next line break ... and so on. After that you have a new string including line breaks which can be displayed. – andre baresel Mar 14 '14 at 13:37
  • Try with JTextArea is much simpler :-) – andre baresel Mar 14 '14 at 13:38