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.
Related
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.
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
I'm currently searching for a method to display an introduction for my program. Every part of the program has it's own instructions that are several lines long. At the end of each line there is \n\n for formatting.
It would be cool if the font size could adjust automatically to the panel size and if the text could be centered. I tried TextLayout and adding the text to a simple JLabel but none of this displays the text correctly.
Any suggestions on how to accomplish this task?
You can also use HTML formatting in the JLabel text. All you need to do is surround the text with <html> and </html>, set the font size with a <font> element, and replace every \n with <br>.
String formatted = text.replace("\n", "<br>");
formatted = "<html><font size='9'>" + formatted + "</font></html>";
JLabel label = new JLabel(formatted);
See How to Use HTML in Swing Components.
I agree with #Josh M, you should try using a JTextArea with a JScrollPane. Alternatively, if you do not expect too much GUI manipulation by the user, you could manually format the text using basic stuff like newlines ('\n') and tabs('\t').
You can give a columns count to your JTextField like this:
JTextField textField = new JTextField("I Have a Big Text and I want show it now!", 40);
40 is the column index which means the text has more columns for showing data in default now.
Another way you could do this is to use JTextArea
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.
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