-1

I can successfully wrap normal HTML text in a JLabel. But when I use pre it doesn't seem to wrap.

Here's what I have set the text in my JLabel to:

<html><pre face=\"Arial\" style = \"white-space:pre-wrap;\">" + temp +  "</pre></html>

this same html code works when I test it on w3school's tryit editor (https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_textarea_wrap)

Please provide the simplest solution possible.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    *"Please provide the simplest solution possible."* Please ask a question. – Andrew Thompson Jun 11 '18 at 19:48
  • 1
    BTW - Swing's support for CSS is very patchy & rudimentary. It simply does not support `white-space:pre-wrap;`. – Andrew Thompson Jun 11 '18 at 19:49
  • "this same html code works when I test it on w3school's" - this is a WRONG statement. You have just overlooked that it displays
    .
    – mentallurg Jun 11 '18 at 20:01
  • @mentallurg I don't understand what you are saying, can you please clarify? – Jarred Mann Jun 11 '18 at 21:20
  • @AndrewThompson my quesion is how do I wrap my JLabel using
     in html. Isn't it obvious?
    
    "It simply does not support white-space:pre-wrap;" - ok, thanks. can you please provide another solution for me? I absolutely have to use html as I'm using a combination of text and images (ie: when one types certain syntax such as "WS" then "WS.png" will be displayed in place).
    – Jarred Mann Jun 11 '18 at 21:26
  • *"Isn't it obvious?"* It is when you [edit] the question to **include that information.** – Andrew Thompson Jun 12 '18 at 03:18
  • @AndrewThompson I still feel the question shows that I'm looking to try and wrap using
     in Java. that being said, let's move on from this, apologies if I came across offensive. People on this site appear rude to me so it's quite a culture shock. 
    
    anyway, is there another solution I could use? to clarify:
    
    - must be html, reason is using images and text combined by means of keywords (as my WS example above).
    - needs to work like "pre" in that I can use spacebar instead of &nbsp
    - needs to wrap
    
    thank you
    – Jarred Mann Jun 12 '18 at 07:45
  • Preformatted text was never intended to word wrap. I can't speak to the W3 Schools (AKA W3 fools) test. An HTML construct that can perhaps give the effect you seem to be wanting is an HTML ` – Andrew Thompson Jun 12 '18 at 10:25
  • @AndrewThompson thanks for your reply. a textarea can't use html from what I know. I googled and tried myself and didn't seem to find a way to do that? the effect I am looking for is this: my project is a card creator for a card game I'm inventing (hence my poor programming skills, haven't touched java since high school). the point of using html is to be able to type in a textarea, "Deal 1 dmg" and instead of "dmg" it will show an icon (on the Jlabel or whatever else). I've already done the "dmg" thing successfully. – Jarred Mann Jun 12 '18 at 18:49
  • @AndrewThompson pre text can wrap in html from what I can see? https://longren.io/wrapping-text-inside-pre-tags/ the point of
     is that it was difficult to format the cards using &nsbp and 
    etc. So I found with
     one can just type and use spaces via the spacebar and enter for a new line. And I did see you can wrap 
     in html but as you said above, Swing's support for CSS is very patchy.
    – Jarred Mann Jun 12 '18 at 18:52
  • jEditorPane and jTextPane work with the html but I'm not sure how to wrap them. I'm looking online but, please forgive my ignorance, I'm doing this app once-off just because my team needs it, and this is my final task to finish. Is there a simple solution you can provide to wrap a JEditorPane/JTextpane please? – Jarred Mann Jun 12 '18 at 19:00

1 Answers1

0

The whole purpose of <pre> is to keep the text formatting as is. pre is for "preserve formatting". It will never wrap.

On the page you named it could not work. Probably you have placed your code inside the <textarea>? The <textarea> interprets <pre> not as a tag, but as a normal text and you saw also the <pre> in the result window. Didn't you? May be you have just overlooked it.

mentallurg
  • 4,967
  • 5
  • 28
  • 36
  • "On the page you named it could not work.Probably you have placed your code inside the – Jarred Mann Jun 11 '18 at 21:22