Examples of Java JTextPane
Following are the programs to implement Java JTextPane
Example 1:
Java
// Java Program to demonstrate a simple JTextPane import javax.swing.*; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; public class JTextPaneExample { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { // Create a JFrame JFrame frame = new JFrame( "JTextPane Example" ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.setSize( 400 , 300 ); // Create a JTextPane JTextPane textPane = new JTextPane(); // Step 2: Create styled text SimpleAttributeSet attributes = new SimpleAttributeSet(); StyleConstants.setBold(attributes, true ); StyleConstants.setItalic(attributes, true ); textPane.setCharacterAttributes(attributes, false ); // Step 3: Set the text textPane.setText( "This is a JTextPane example with styled text. !!Geeks Premier League 2023!!" ); // Step 4: Add JTextPane to the frame frame.add( new JScrollPane(textPane)); frame.setVisible( true ); }); } } |
Output:
Example 2:
Java
// Java Program to Implement setStyledDocument, // getStyledDocument, insertComponent methods of the // JTextPane import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class JTextPaneExample { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { // Create a JFrame JFrame frame = new JFrame( "JTextPane Example" ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.setSize( 400 , 300 ); // Create a JTextPane JTextPane textPane = new JTextPane(); // Create a custom StyledDocument StyledDocument doc = textPane.getStyledDocument(); // Create and set a Style for the text Style style = doc.addStyle( "customStyle" , null ); StyleConstants.setForeground(style, Color.BLUE); StyleConstants.setBold(style, true ); // Set the StyledDocument textPane.setStyledDocument(doc); // Insert text with custom style try { doc.insertString( 0 , "This is a " , style); doc.insertString(doc.getLength(), "button: " , null ); } catch (BadLocationException e) { e.printStackTrace(); } // Insert a button as a Swing component JButton button = new JButton( "Click Me!" ); button.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog( frame, "Button clicked!" ); } }); textPane.insertComponent(button); // Add JTextPane to the frame frame.add( new JScrollPane(textPane)); frame.setVisible( true ); }); } } |
Output:
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. |
Contact Us