JAVA - set font of selectedtext in a JEditorPane - java

How can I set the font/bold/italic/underline whatever of some text in a JEditorPane?
I am trying to create a simple document editor, but i can only figure out how to set the font/bold/italic/whatever of the ENTIRE JEditor/JText pane.
RTF is preferred, HTML is fine as well.

JEditorPane.getDocument() returns an instance of StyledDocument. StyledDocument has setCharacterAttributes() method to set the font properties.
Related example: http://www.java2s.com/Code/JavaAPI/javax.swing/JTextPanesetCharacterAttributesAttributeSetattrbooleanreplace.htm

Related

Javafx get font style for a given font

I want to get font style(bold, italics...) for the given fonts.
Currently i am getting the font list in a combobox using javafx.scene.text.Font.getFamilies() and now I want to get font style for each font, like that in notepad.
can any one guide me how to get list of those fonts style and use that to set the property for a given TextArea named demo_text_area.

Jtextpane copy coloured text and paste

I have a JTextPane which displays colored text . I use the followong piece of code to get text from JTextPane.
String temp = pane.getDocument().getText(0,pane.getDocument().getLength());
However when i try to set the temp variable content to pane again,
pane.select(0,pane.getDocument().getLength());
pane.replaceSelection(temp);
by this way i lose the color and get white text. Is there anyway where i can maintain the color of text without copying the content to clipboard.
Please help.
Actually it depends on EditorKit you use. The first part returns text (with styled info) of the selected fragment. E.g. in the RTFEditorKit it would be rtf content of the document's fragment.
The second part is not correct. Replace selection can't process the content properly. I guess in case of the RTFEditorKit it would be all the text with formatting inserted in the pane.
I would use
pane.setText(temp);
instead. If you need to insert the styled fragment use kit.read(...) passing the temp in the call
You can try the Kit as alternative of the default RTFEditorKit and see what happens
UPDATE: Sorry original comment was incorrect a bit. The code should be
pane.getEditorKit().read(
new StringReader(temp),
pane.getDocument(),
pane.getDocument().getLength())

How to keep font unchanged when copied to a JEditorPane in java?

I have a JEditorPane where I'll copy some text from other application e.g. Adobe Pagemaker, Corel Draw etc. I want to keep my fonts unchanged. How can I do it ?
I want a JEditorPane with different font.
The examples in How to Use Editor Panes and Text Panes may guide you in this. In particular, TextComponentDemo illustrates changing font characteristics both programmatically and interactively. See initDocument() for details.
You have to set it in StyleContext, a default font and then set the StyleContext object for your JEditorPane
Read about StyleContext

Java apply HTML to a selected text

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)

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