
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
Get Text from JTextPane and Display in Console
In this article, we will learn how to get text from a JTextPane in Java and display it in the console. We'll use the getText() method to retrieve the text and show it in the console. Additionally, we'll apply simple text styling like italics and color using SimpleAttributeSet to demonstrate how to manage styled text in a GUI.
Steps to get text from JTextPane
Following are the steps to get text from JTextPane and display it in the Console using Java ?
- First, we will start by creating a JFrame to serve as the main window of the GUI.
- We will set the default close operation for the frame to ensure the application closes when the window is closed.
- Create a JTextPane component, which will hold the text.
- Use a SimpleAttributeSet to apply text styles like italics, blue color, and white background using StyleConstants.
- Set text in JTextPane by using the setText() method to insert the text "This is our demo text! We are displaying the text in the console." into the text pane.
- Get Text from JTextPane to retrieve the text from the JTextPane using the getText() method and print it to the console.
- Add the scroll pane and wrap the JTextPane inside a JScrollPane to allow scrolling if necessary, and add it to the frame.
- Display the frame we will set the size of the frame with setSize(550, 300) and make it visible using setVisible(true) to display the window.
Java program to get text from JTextPane
The following is an example to get JTextPane and display it in the Console ?
package my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; 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.StyleConstants; 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(); SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setItalic(attributeSet, true); StyleConstants.setForeground(attributeSet, Color.blue); StyleConstants.setBackground(attributeSet, Color.white); textPane.setCharacterAttributes(attributeSet, true); textPane.setText("This is our demo text! We are displaying the text in the console."); System.out.println(textPane.getText()); JScrollPane scrollPane = new JScrollPane(textPane); container.add(scrollPane, BorderLayout.CENTER); frame.setSize(550, 300); frame.setVisible(true); } }
Output
The text would be visible on the Console ?
Code explanation
In this program, a JTextPane is created to display and style text. The text is set to "This is our demo text! We are displaying the text in the console." using the setText() method. To retrieve and display this text in the console, we use the getText() method and print the result. A SimpleAttributeSet is applied to style the text with blue color, italics, and a white background. The text pane is then wrapped inside a JScrollPane and added to a JFrame to be displayed in a window.