Set different font formats in jTextArea [duplicate] - java

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.

Related

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

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

Text formatting in JEditorPane

I need to change the style(i.e font,color and other attributes) of a particular word or line.
I have tried this with JTextPane as:
textPane.getStyledDocument().setCharacterAttributes(start,length,myTextStyle,false);
Is there any way to do the same thing with JEditorPane.
How can i format a word in JEditorPane.
Cast it to styled document
((StyledDocument)editorPane.getDocument()).setCharacterAttributes(start,length,myTextStyle,false);
You can use HTMl or RTF text to format text within a JEditorPane.
There is a good explanation on how to do this # http://docs.oracle.com/javase/tutorial/uiswing/components/text.html
Mind, the naming, "JEditorPane" is misleading; JTextPane is a subclass of JEditorPane. So if you want to make your own styled editor, use JTextPane.

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