- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
I want to highlight all the text in the Java Swing Control Text Pane
To highlight all the text, use the selectAll() method of the JTextPane component −
JTextPane pane = new JTextPane(); pane.selectAll();
The following is an example to highlight the JTextPane text. Here, we are displaying a code in the JTextPane and highlighting it −
Example
package my; import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; 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 pane = new JTextPane(); pane.setContentType("text/html"); pane.setText("<HTML><BODY><CODE> #include <iostream>; <br> using namespace std; <br>main() {<br>cout << \"Hello World\";<br> return 0;<br>}</CODE><br></BODY></HTML>"); pane.selectAll(); JScrollPane scrollPane = new JScrollPane(pane); container.add(scrollPane, BorderLayout.CENTER); frame.setSize(550, 300); frame.setVisible(true); } }
Output
- Related Articles
- How to highlight text in a tkinter Text widget?
- Java Program to create JCheckBox from text in Swing
- How to highlight the current line of a Text widget in Tkinter?
- How to highlight a row in a table with Java Swing?
- How to highlight multiple rows in a sequence in a table with Java Swing?
- Highlight a text, every time page loads with JavaScript
- How do I center the text in a Tkinter Text widget?
- How can I append text to JTextArea in Java?
- Control the flow and formatting of text with CSS
- Make a custom cursor appear when the user moves the mouse over some text in a Java Swing JDialog
- How to highlight text inside a plot created by ggplot2 using a box in R?
- How to wrap the text in text flow layout in JavaFX?
- Select all text in a Text widget using Python 3 with tkinter
- Convert All Lowercase Text of a File into Uppercase in Java?
- How to insert text at the beginning of the text box in Tkinter?

Advertisements