Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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

Advertisements
