- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to insert a component into a JTextPane component
Let’s say the following is our JTextPane −
JTextPane textPane = new JTextPane(); textPane.setForeground(Color.white); textPane.setBackground(Color.blue);
Now, set the style and attributes. Also, set the font −
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);
After the text displayed above, we will insert a component using setComponent() −
StyledDocument doc = (StyledDocument) textPane.getDocument(); Style style = doc.addStyle("StyleName", null); StyleConstants.setComponent(style, new JButton("Submit")); doc.insertString(doc.getLength(), "invisible text", style);
The following is an example to insert a component into a component. Here, we have set a button component inside a JTextPane −
Example
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
- Related Articles
- Java Program to insert styled text in a JTextPane Component
- Inserting an Image into a JTextPane Component with Java
- How to create a JMenuBar Component in Java?
- How to add a tooltip to a component in Java?
- Set a component and place it next to the previously added component with GridBagLayout in Java
- How to inject a service into the component in Angular 8?
- How to create a compound border for a component in Java?
- How to find a node in a JTree Component with Java?
- How to set vertical alignment for a component in Java?
- How to create Titled Border for a component in Java?
- Java Program to remove file information from a filename returning only its path component
- Java Program to remove path information from a filename returning only its file component
- Java Program to Insert a string into another string
- Java Program to place component in bottom-right corner with BorderLayout
- How do I test a Python program or component?

Advertisements