
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Insert Component into JTextPane in Java
In this article, we will learn how to add a component to a JTextPane in Java. By using StyledDocument and StyleConstants, we can insert elements like buttons within the text pane, allowing us to create dynamic and interactive text-based components.
JTextPane
JTextPane is a versatile text component in Java Swing that allows for styled text. It supports multiple text formats like bold, italic, and different fonts. It can also display rich text, such as embedded images or buttons, through the StyledDocument class.
Inserting a component into a JTextPane
The following are the steps to insert a component into a JTextPane ?
-
Step 1. Create a JFrame window: A new JFrame named "Demo" is created. This window will be the main container for the GUI.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ensures that the application exits when the window is closed.
JFrame frame = new JFrame("Demo");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
Step 2. Get the content pane of the frame: getContentPane() retrieves the main container of the JFrame where components like buttons, text fields, and panels can be added
Container container = frame.getContentPane();
-
Step 3. Create a JTextPane: JTextPane is created as a text component that allows styled text. setForeground(Color.white) sets the text color to white. setBackground(Color.blue) sets the background color to blue.
JTextPane textPane = new JTextPane(); textPane.setForeground(Color.white); be> textPane.setBackground(Color.blue);
-
Step 4. Create SimpleAttributeSet for text styling: A SimpleAttributeSet object is created to store character-level attributes for text styling.
StyleConstants.setItalic(attributeSet, true) sets the text to be italic.
SimpleAttributeSet attributeSet = new SimpleAttributeSet();StyleConstants.setItalic(attributeSet, true);
-
Step 5. Set the character attributes of the JTextPane: This applies the created attributeSet (which includes the italic style) to the JTextPane. The true argument means it applies the attributes to the entire text.
textPane.setCharacterAttributes(attributeSet, true);
-
Step 6. Set the text in the JTextPane: This sets the initial text to "Press the Button" in the JTextPane.
textPane.setText("Press the Button ");
-
Step 7. Set the font of the text: A Font object is created with the font "Verdana", bold style, and size 22.
setFont(font) applies this font to the text in JTextPane.
Font font = new Font("Verdana", Font.BOLD, 22);
textPane.setFont(font);
-
Step 8. Create a StyledDocument to insert components: getDocument() returns the StyledDocument of the JTextPane, which allows you to insert styled text and components. A new Style is created and named "StyleName", which can later be used to define the appearance of components added to the document.
StyledDocument doc = (StyledDocument) textPane.getDocument();
Style style = doc.addStyle("StyleName", null);
-
Step 9. Create a JButton and add it to the document: A new JButton with the label "Submit" is created and added to the StyledDocument with the style defined earlier. This embeds the button into the text.
StyleConstants.setComponent(style, new JButton("Submit"));
-
Step 10. Insert some invisible text to demonstrate component insertion: This inserts the string "invisible text" at the end of the document, applying the style which contains the embedded button. However, the text is not visible as it has no appearance, just the button.
doc.insertString(doc.getLength(), "invisible text", style);
-
Step 11. Wrap JTextPane inside a JScrollPane to make it scrollable: JScrollPane is used to make the JTextPane scrollable if the text exceeds the visible area. The JScrollPane is added to the center of the frame's content pane.
JScrollPane scrollPane = new JScrollPane(textPane);
container.add(scrollPane, BorderLayout.CENTER);
-
Step 12. Set the size of the frame and make it visible: setSize(550, 300) sets the dimensions of the window (550 pixels wide by 300 pixels tall).
setVisible(true) makes the frame visible on the screen.
frame.setSize(550, 300);
frame.setVisible(true);
Java program to insert a component into a JTextPane
The following is an example of inserting a component into a JTextPane ?
package my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; public class SwingDemo { public static void main(String args[]) throws BadLocationException { JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container container = frame.getContentPane(); JTextPane textPane = new JTextPane(); textPane.setForeground(Color.white); textPane.setBackground(Color.blue); SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setItalic(attributeSet, true); textPane.setCharacterAttributes(attributeSet, true); textPane.setText("Press the Button "); Font font = new Font("Verdana", Font.BOLD, 22); textPane.setFont(font); StyledDocument doc = (StyledDocument) textPane.getDocument(); Style style = doc.addStyle("StyleName", null); StyleConstants.setComponent(style, new JButton("Submit")); doc.insertString(doc.getLength(), "invisible text", style); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane = new JScrollPane(textPane); container.add(scrollPane, BorderLayout.CENTER); frame.setSize(550, 300); frame.setVisible(true); } }
Output
Advertisements