how can i use tabulator\whitespace in JTextPane using html? - java

I wish to use some tabspace in my JTextPane using html, how can i do it ?
I tried the Java way with putting \t, however that seems not to work.
this is my code:
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
String ausgabe = "hello world";
HTMLDocument doc=(HTMLDocument) pane.getStyledDocument();
doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()),"<b>"+ausgabe"+"<br></b>");

Generally, HTML does not display formatting such as indentation, tabs, etc. You can check that in a plain HTML file in a browser.
However, there is one circumstance where it does - in a preformatted block. That is, with the tag <pre>.
So try changing that last line to:
doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()),"<pre>"+ausgabe+"</pre>");
Note that the font used in preformatted blocks is usually fixed-width (e.g. Courier or a similar font).

As an alternative solution, you can also insert 4 times the character

Related

StringEscapeUtils is unescaping everything, except newlines (\n)

I try to escape and unescape large text using the StringEscapeUtils from Apache Commons library (v. 1.7) that will be stored and retrieved from a database, in this case a H2 database. Almost every special character is escaped and unescaped successfully except for the new line. I am using Spring Boot (v2.1.3.) with thymeleaf.
So for instance, if I try to store the following text:
He didn't say, "Stop!"
This is a new line!
It will store the text as:
He didn't say, \"Stop!\"\r\n\r\nThis is a new line!
Which is good. But when I unescape it with the unescapeJava method the new line character is not working correctly. I get
He didn't say, "Stop!" This is a new line!
Edit:
The unescapeJava method works when the text is displayed in a html textarea. But when it is rendered as plain html, the linebreak is not working.
The unescapeJava method works when the text is displayed in a html textarea. But when it is rendered as plain html, the linebreak is not working.
Please check your HTML source, it most likely is there
In HTML a newline in source does not introduce a newline on screen, if you want that you should replace newlines with for example <br/> tags.
See more at:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/br
What #Martin van Wingerden says is correct. A newline in source does not introduce / render it on screen. I found this thread
Render a string in HTML and preserve spaces and linebreaks
which explains that you can use CSS white-space: pre-wrap that will preserve white spaces and line breaks in formatting.

Set different font formats in jTextArea [duplicate]

Can I alter the text of a JTextArea to bold (append text) and then back to normal and will it only display the bold text in bold and the rest as normal?
Also can the contents of JTextArea be saved as an RTF document?
No. What you're looking for is JEditorPane
This supports HTML (3.2?) which will allow you to use <font> (and other older tags) to provide rich text.
JEditorPane textarea = new JEditorPane("text/html", "");
textarea.setText("Here is some <b>bold text</b>");
EDIT: According to the javadoc I referenced above, JEditorPane also supports limited RTF. Don't forget to change the MIME to text/rtf
textArea.setFont(textArea.getFont().deriveFont(Font.BOLD, textArea.getFont().getSize()));
I believe you need a JTextPane or JEditorPane for that.

How to draw line in JTextArea and change color of a specific words

I'm currently developing JTextArea with a Notepad++ like function (currently indentation function finished).
Now I'm trying to add a function which in my JTextArea will show a dotted line for an indentation with same level, and change specific color of a word.
Thanks in advance :)
You will not able to achieve your target with JTextArea as it is a plain text support component.You have to use a Styled text supported JEditorPane or JTextPane.And try to use a HTML document and achieve it.Look at
Styled supported components
and
some more examples

Set a string to bold in JTextArea?

I don't want to set the entire text area to bold but just a selected single line. How would go about doing that?
I suggest using a JTextPane instead as there are example solutions for it:
http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html
http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample6.htm
There is no way to do it with JTextArea. You can achieve this with JEditorPane.
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setText("<b>This text is bold</b>");
According to the documentation of JTextArea,
A JTextArea is a multi-line area that displays plain text.
plain text, in this sense, means every character is formated the same way. There is no way to format some characters differently than other characters.

Java JTextPane Change Font of Selected Text

I have a JTextPane (or JEditorPane, I can use either no problem). How can I change the font of a selected area to a specific font?
textpane.getSelectedText().setFont() won't work. (Even with font-family)
You can change JTextPane's font only as a whole, it doesn't do rich text.
There's a Document underneath JEditorPane (and apparently JTextPane too), which you get a hold of with getDocument(). You want to cast that to a StyledDocument if you can, and then you can do things like setCharacterAttributes to a given run of characters.
There are some (hopefully) helpful examples in the Java tutorial at http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html .
You can do this by using JTextPane. It is impossible to do this using JTextArea. . Here is a best example on how to use JTextPane.
Sample: http://download.oracle.com/javase/tutorial/uiswing/components/editorpane.html
Code: http://download.oracle.com/javase/tutorial/uiswing/examples/components/TextSamplerDemoProject/src/components/TextSamplerDemo.java

Categories

Resources