Methods of JTextPane

Methods

Description

setText(String text)

Sets the text content of the JTextPane.

getText()

Retrieves the text content of the JTextPane.

setStyledDocument(StyledDocument doc)

Sets the StyledDocument to manage the text content and styles.

getStyledDocument()

Retrieves the current StyledDocument used by the JTextPane.

insertComponent(Component comp)

Inserts a Swing component at the current caret position in the text pane.

insertIcon(Icon icon)

Inserts an icon at the current caret position in the text pane.

setCharacterAttributes(AttributeSet attr, boolean replace)

Applies character attributes to the selected text or at the current caret position.

replaceSelection(String content)

Replaces the currently selected text with the specified content.

getSelectedText()

Retrieves the currently selected text in the text pane.

cut()

Cuts the selected text and places it in the clipboard.

copy()

Copies the selected text to the clipboard.

paste()

Pastes the content from the clipboard into the text pane.

replaceSelection(String content)

Replaces the currently selected text with the specified content.

getContentType()

Returns the content type of the text pane.

getCaret()

Retrieves the caret (cursor) position within the text pane.

setCaretPosition(int position)

Sets the caret position to the specified character index.

Java JTextPane

In Java, JTextPane is a versatile component that is part of the Swing library for building graphical user interfaces. It extends JEditorPane and provides an editable text component with rich text formatting capabilities. JTextPane allows you to display and edit styled text, making it suitable for implementing text editors, document viewers, and other applications that require advanced text formatting. In Java JTextPane is a part of javax.swing package.

The constructor of Java JTextPane

Constructor

Description

JTextPane()

This is the default constructor that creates an empty JTextPane.

JTextPane(StyledDocument doc)

This constructor allows you to specify a pre-existing StyledDocument to be used as the document model for the JTextPane.

JTextPane(String text)

Creates a JTextPane with the specified initial text. The text is not styled in this case.

Similar Reads

Methods of JTextPane

Methods Description setText(String text) Sets the text content of the JTextPane. getText() Retrieves the text content of the JTextPane. setStyledDocument(StyledDocument doc) Sets the StyledDocument to manage the text content and styles. getStyledDocument() Retrieves the current StyledDocument used by the JTextPane. insertComponent(Component comp) Inserts a Swing component at the current caret position in the text pane. insertIcon(Icon icon) Inserts an icon at the current caret position in the text pane. setCharacterAttributes(AttributeSet attr, boolean replace) Applies character attributes to the selected text or at the current caret position. replaceSelection(String content) Replaces the currently selected text with the specified content. getSelectedText() Retrieves the currently selected text in the text pane. cut() Cuts the selected text and places it in the clipboard. copy() Copies the selected text to the clipboard. paste() Pastes the content from the clipboard into the text pane. replaceSelection(String content) Replaces the currently selected text with the specified content. getContentType() Returns the content type of the text pane. getCaret() Retrieves the caret (cursor) position within the text pane. setCaretPosition(int position) Sets the caret position to the specified character index....

Examples of Java JTextPane

Following are the programs to implement Java JTextPane...

Contact Us