
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Inserting an Image into a JTextPane Component with Java
Let’s say the following is our JTextPane with orange background color −
JTextPane textPane = new JTextPane(); textPane.setBackground(Color.orange);
Now, set the style and attributes. Also, set the font −
SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setItalic(attributeSet, true); textPane.setCharacterAttributes(attributeSet, true); textPane.setText("Recall this and "); Font font = new Font("Verdana", Font.BOLD, 22); textPane.setFont(font);
After the text displayed above, we will insert an image using setIcon() −
StyledDocument doc = (StyledDocument) textPane.getDocument(); Style style = doc.addStyle("StyleName", null); StyleConstants.setIcon(style, new ImageIcon("E:\\kind.png")); doc.insertString(doc.getLength(), "invisible text", style);
The following is an example to insert an image into a component. Here, we have inserted an image into a JTextPane component −
Example
package my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import javax.swing.ImageIcon; 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.setBackground(Color.orange); SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setItalic(attributeSet, true); textPane.setCharacterAttributes(attributeSet, true); textPane.setText("Recall this and "); Font font = new Font("Verdana", Font.BOLD, 22); textPane.setFont(font); StyledDocument doc2 = (StyledDocument) textPane.getDocument(); Style style2 = doc2.addStyle("StyleName", null); StyleConstants.setIcon(style2, new ImageIcon("E:\\kind.png")); doc2.insertString(doc2.getLength(), "invisible text", style2); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane = new JScrollPane(textPane); container.add(scrollPane, BorderLayout.CENTER); frame.setSize(550, 300); frame.setVisible(true); } }
Output
- Related Questions & Answers
- Java program to insert a component into a JTextPane component
- Java Program to insert styled text in a JTextPane Component
- Inserting an Element into Deaps
- Inserting multiple parameter values into a single column with MySQL?
- Inserting records into a MySQL table using PreparedStatement in Java?
- Inserting a key into a tree in Javascript
- Disable Tooltip for a component with an already enabled tooltip in Java
- Inserting random numbers into a table in MySQL?
- Turn an image into a Bootstrap 4 card background
- Inserting Array list into HANA database
- Inserting Records into Table using Node
- Write an JDBC example for inserting value for Blob datatype into a table?
- Inserting data into a new column of an already existing table in MySQL?
- How to place an image into a frame in Tkinter?
- Write an JDBC example for inserting value for Clob data type into a table?
Advertisements