Multiple fonts and styles in JTextComponent - java

I am looking for a way to print content in JTextComponent's text with multiple fonts and styles.
For example: first Title with bigger font and then some custom text in different style in one component

You can use a JTextPane for this. You can control font, color, bold, size etc.
Read the section from the Swing tutorial on Text Component Features for more information and working examples.
For a another simple example to get you started check out: Is there a way to filter / search for content in a HTMLEditorKit?

Related

Swing JTextArea cannot show font type and color

I am developing a spam detector application using Java GUI (NetBeans). Now I have a simple user interface which includes a text area in which the user is going to paste his email in to order to check if its a spam or not, but the problem is when you paste a text in the text area all the text properties will be lost such as : Font type, color, hyperlinks etc...
So what should I do?
That's because you need RTF-enabled Swing component to visualize various text properties.
JTextArea is visualizing only plain text. Use JEditorPane or JTextPane instead.

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

Different text color in a JTextArea

Is it possible to have different rows in different colors in a textarea? Have you an example for that?
Not with a JTextArea. You can, however, use a JTextPane which allows for attributes to be applied to text. If you want examples, take a look at the Text Component Tutorial.

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.

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