- 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 wrap text in a JTextPane and show Scrollbar
Let’s say we have lots of content in our JTextPane component −
textPane.setText("This is demo text1. This is demo text2. This is demo text3." + "This is demo text4.This is demo text5. This is demo text6. " + "This is demo text7. This is demo text8. This is demo text9. " + "This is demo text10. This is demo text11. This is demo text12." + "This is demo text13. This is demo text13. This is demo text14." + "This is demo text15. This is demo text13. This is demo text16." + " This is demo text17. This is demo text13. This is demo text18." + " This is demo text19.This is demo text13.This is demo text20.");
To wrap it and display the scrollbar, set scrollpane and use the VERTICAL_SCROLLBAR_AS_NEEDED constant −
JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
Example
package my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; 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(); textPane.setBackground(Color.BLUE); Font font = new Font("Serif", Font.ITALIC, 22); textPane.setFont(font); 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 demo text1. This is demo text2. This is demo text3." + "This is demo text4.This is demo text5. This is demo text6. " + "This is demo text7. This is demo text8. This is demo text9. " + "This is demo text10. This is demo text11. This is demo text12." + "This is demo text13. This is demo text13. This is demo text14." + "This is demo text15. This is demo text13. This is demo text16." + " This is demo text17. This is demo text13. This is demo text18." + " This is demo text19.This is demo text13.This is demo text20."); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); container.add(scrollPane, BorderLayout.CENTER); frame.setSize(500, 300); frame.setVisible(true); } }
Output
- Related Articles
- Java Program to format text in JTextPane
- Java Program to insert styled text in a JTextPane Component
- Java Program to get text from JTextPane and display in Console
- 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 can we implement line wrap and word wrap text inside a JTextArea in Java?
- How to set font face, style, size and color for JTextPane text in Java
- Java program to insert a component into a JTextPane component
- How to attach a Scrollbar to a Text widget in Tkinter?
- How to word-wrap text in Tkinter Text?
- Python program to wrap a text into paragraph with width w
- How to attach a vertical scrollbar in Tkinter text widget?
- Java Program to wrap a Primitive DataType in a Wrapper Object
- Java Program to set JTextArea to wrap by word in Java
- How to wrap the text in text flow layout in JavaFX?

Advertisements