JEditorpane vs. JTextPane - java

i have to create a project in java swing for my college. An editor of java files with proper text highlighting i.e different colors and fonts for java keywords, java comments and for normal text.
Help me to select one of two styled text component JEditorpane and JTextPane provided by java so that i can full-fill the requirements.
Please tell me suitable difference between these two that in which kind of situation i have to use one of these.

Although both support rich text. But there is difference.
JEditorPane supports display/editing of HTML.
JTextPane is an extension of JEditorPane which provides word processing features like fonts, text styles, colors, etc.

You can use either of them, but if it is a rich text editor then I would suggest using the JTextPane.
You may also find this topic useful.

Related

Format an editable textarea

At the moment I try to find a method to create a TextArea that I can edit and simply (without cheating) apply syntax highlighting to. Is that possible without the need of a custom Component? I already managed to format a text using a JEditorPane, but I am not sure how to implement that the text is dynamicly highlighted... And that efficiently. Is that possibly without an enormous amount of coding?
You can't use a JTextArea since it does not support text attributes.
Instead you can use a JTextPane. Read the section from the Swing tutorial on Text Component Features for more information and examples.

How do i format a specific word/sentence inside jeditorpane?

I am not a java expert. Just learning as i go.. This is my way! I am now designing a IDE for C++ (just fun, not professional) . I have the project almost ready, now i want to add some text highlighting function to the IDE. For example i want the IDE to recognize a predefined set of words and color them green,red. How do i do it?
You could use HTML, but you probably would be better off using the TextAction methods for a JEditorPane.
Another alternative is the StyledDocument interface.
Added because of the comment: You can use the StyledEditorKit class to see implementations of TextAction.
Here's Oracle's tutorial on How to Use Editor Panes and Text Panes.

Text editor in Java with sample functions for desktop application

I have a project, I'm creating an application in Java and I need text editor with sample functions (Bold, Italic, Size, Align, Undo, Redo).
So is there a class in Java for this purpose or a tutorial to show how to develop it?
I tried to style text in a jTextPane but when I change a word in bold, the whole text become in bold.
I want just to stylize the selected text only.
This should get you started: Editor based on JTextPane
Or this tutorial: How to Use Editor Panes and Text Panes
(source: oracle.com)
The Java Tutorial's TextComponentDemo illustrates many features such as undo/redo, styling selected text and binding key strokes to editing actions.

Formatting text with HTML in a JEditorPane?

I am trying to make a simple email client in Java Swing.
I want to allow users to format their email in any way they want, like making some parts of the text bold, other parts italic, etc. In other words, I am trying to make a WYSIWYG editor. The formatting is done in HTML. I am using JEditorPane to display the text.
I have tried adding tags myself to the text directly by using setText and getText methods of JEditorPane. I could make it work for basic formatting, but it is quite difficult to handle complex formatting. (trying to remove tags from multi-tagged elements, for example)
Is there an easier way to accomplish this? I have looked at HTMLEditorKit but it seems like it does not support adding tags to and/or replacing a specific string.
Thanks in advance.
The HTMLEditorKit comes with some default Actions that allow you to do some basic styling of the text with the click of a menu item (or button). Take a look at the example in the section from the Swing tutorial on Text Component Features.

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