- 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 get text from JTextPane and display in Console
To get text from JTextPane, use the getText() method. At first, create a text pane component −
JTextPane textPane = new JTextPane();
Now, get the text −
textPane.getText()); Display in Console: System.out.println(textPane.getText());
The following is an example to get JTextPane and display in Console −
Example
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 Console −
- Related Articles
- Java Program to format text in JTextPane
- Java Program to wrap text in a JTextPane and show Scrollbar
- Java Program to insert styled text in a JTextPane Component
- Get number from user input and display in console with JavaScript
- How to read data from PDF file and display on console in Java?
- Java Program to set the Font and Color of some text in a JTextPane using Styles
- How to set font for text in JTextPane with Java?
- How to set font face, style, size and color for JTextPane text in Java
- How to get Exception log from a console and write it to external file in java?
- Ways to read input from console in Java
- Read integers from console in Java
- Java Program to create JRadioButton from text
- How can we get the values of a JProgressBar Component and display in Console?
- Java program to insert a component into a JTextPane component
- Java Program to create JCheckBox from text in Swing

Advertisements