setting JTextPane to content type HTML and using string builders

Every time JTextPane.setText(…) is called a new content type is determined. Start the text with “<html>” and you’ve got HTML. A new document is created, in your case HTMLDocument. @mKorbel: the following creates every time HTML for the JTextPane. buildSomething.append(“<html>”); buildSomething1.append(“<html>”); for (int i = 0; i < 10; i++) { buildSomething.append(“<span style=\”color:red\”>” + myBirthday … Read more

JTextPane highlight text

As often there are several possibilities, depending on what you really mean by “highlight”:-) Highlight by changing any style attributes of arbitrary text parts on the document level, something like SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setForeground(sas, Color.YELLOW); doc.setCharacterAttributes(start, length, sas, false); Highlight via a Highlighter on the textPane level: DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW); textPane.getHighlighter().addHighlight(startPos, … Read more

JEditorPane with Javascript and CSS support

+1 to mKorbel. Use JavaFX WebView which supports HTML5 by integrating it with Swing. Here is an example: import java.awt.*; import java.awt.event.*; import java.net.MalformedURLException; import java.net.URL; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import static javafx.concurrent.Worker.State.FAILED; import javafx.embed.swing.JFXPanel; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.web.WebEngine; import javafx.scene.web.WebEvent; import javafx.scene.web.WebView; import javax.swing.*; public class SimpleSwingBrowser implements Runnable { … Read more

How to add text different color on JTextPane

This will print out “BLAH BLEG” in two different colors. public class Main { public static void main(String[] args) { JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle(“I’m a Style”, null); StyleConstants.setForeground(style, Color.red); try { doc.insertString(doc.getLength(), “BLAH “,style); } catch (BadLocationException e){} StyleConstants.setForeground(style, Color.blue); try { doc.insertString(doc.getLength(), “BLEH”,style); } catch … Read more

How to change the color of specific words in a JTextPane?

No. You are not supposed to override the paintComponent() method. Instead, you should use StyledDocument. You should also delimit the words by your self. Here is the demo, which turns “public”, “protected” and “private” to red when typing, just like a simple code editor: import javax.swing.*; import java.awt.*; import javax.swing.text.*; public class Test extends JFrame … Read more

JTextPane formatting [closed]

Also seen here, TextComponentDemo shows how to apply a number of StyleConstants, including font size, style, alignment and color. The styles may applied either directly to the Document, as shown in initAttributes(), or via the actions of StyledEditorKit, seen here. Addendum: The example below creates three related styles using SimpleAttributeSet. Note that highAlert alters the … Read more

Centering Text in a JTextArea or JTextPane – Horizontal Text Alignment

You need to use a JTextPane and use attributes. The following should center all the text: StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); doc.setParagraphAttributes(0, doc.getLength(), center, false); Edit: Vertical centering is not supported as far as I know. Here is some code you might find useful: Vertical Alignment of JTextPane