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.
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'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
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.
I wanted to know if there is a way that I can apply basic HTML to a text pane or text editor using a Highlighter. My objective is to basically do a text editor using basic HTML. For example a user highlights a part of some text and clicks on a button and I want that to become bold for example, using html but without showing tags or anything, just the selected text in bold. Also I wanted to know if it is better to use a textpane for this or a editorpane and why. Thank you.
anyway, you have to set a HTMLDocument to your JTextPane. Then, you can use the features of HTMLEditorKit and HTMLDocument to achieve what you want. For instance you can use :
insertHTML(jtp.getDocument(), jtp.getSelectionStart(), "<b>"+getSelectedText()+"</b>", 0, 0, HTML.Tag.B);
From the HTMLEditorKit. Something like that (I have not try the code)
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