i was trying to do some simple text formatting using JEditorPane but then as knowledge grew i found JTextPane easier to implement and more robust.
my query is how do i save the formatted text in JTextPane to file? it should be RTF or HTML or other.. as this file is not opened by the application again.
it is a chat history text file with formatted text.
thank you.
You have write method for HTML and RTF from StyledEditorKit. See HTMLEditorKit.write and RTFEditorKit
Related
I am developing a Wordpad application using Java Swing.
I am using JTextPane component. I have added the code for Bold, Italics, Underline. I am adding the code to insert images in all the formats.
My expected scenario: I want to save the document with all these styled content with the extension '.doc' (or)'.docx'. And I want to read and open the document which contains these types of styled content like Bold, Italic, underline text and images, bullet points etc..
I don't think it can be done with HTMLEditorKit().
Can anyone help with the sample code for saving and reading these styled contents to/from external file?
Thanks in advance.
Need a help. Honestly I don't have a clue now how to start on this.
<p>This is a test. Will this text be <b>bold</b> or <i>italic</i></p>
So in my excel, it should be
This is a test. Will this text be bold or italic
Again, I do have example which write this as plain text. Could anyone please help me in making this text as Rich Text in excel. Note: this is just an example. In realtime, it could be any html with styles.
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 have I JTextPane and I'm using DefaultStyledDocument as a text model. I currenly use the JTextPane's default copy() and paste() methods for copying and pasting, but as you know they copy/paste plain text only. I need to be able to copy styled text from a browser for example, and paste it in the JTextPane preserving the styles.
Does anyone know how I can achieve this?
When content in clipboard has representation which can be processed by the installed EditorKit then JTextPane automatically recognize the content and process the content updating Document accordingly.
E.g. if you set RTFEditorKit and paste content copied from MS Word (which has text/rtf flavor) the content will be processed correctly.
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)