The JTextPane Class
JTextPane is a multiline text component that can display text with multiple fonts, colors, and even embedded images. It supports named hierarchical text styles and has other features that can help implement a word processor or a similar application.
Technically, JTextPane is a subclass of JEditorPane, but it usually makes more sense to think of JTextPane as a text component in its own right. JEditorPane uses “editor kits” to handle text in various formats (such as HTML or RTF) in a modular way. Use a JEditorPane when you want to view or edit text in one of these formats. Use a JTextPane when you want to handle the text yourself.
import javax.swing.*; import java.awt.BorderLayout; import java.awt.event.*; // Show how icons, components, and text can be added to a JTextPane. public class PaneInsertionMethods { public static void main(String[] args) { final JTextPane pane = new JTextPane(); // Button to insert some text JButton textButton = new JButton("Insert Text"); textButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { pane.replaceSelection("text"); } }); // Button to insert an icon final ImageIcon icon = new ImageIcon("bluepaw.gif"); JButton iconButton = new JButton(icon); iconButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { pane.insertIcon(icon); } }); // Button to insert a button JButton buttonButton = new JButton("Insert Button"); buttonButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { pane.insertComponent(new JButton("Click Me")); } }); // Layout JPanel buttons = new JPanel(); buttons.add(textButton); buttons.add(iconButton); buttons.add(buttonButton); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(pane, BorderLayout.CENTER); frame.getContentPane().add(buttons, BorderLayout.SOUTH); frame.setSize(360, 180); frame.setVisible(true); } }