Selecting all text in the JTextArea
component can be done by using the selectAll()
method. In the example below we use a button action to do the selection. After select the text we transfer focus back to the JTextArea
so that the highlighted text is shown.
package org.kodejava.example.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextAreaSelectAll extends JPanel {
public TextAreaSelectAll() {
initializeUI();
}
private void initializeUI() {
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(500, 200));
final JTextArea textArea = new JTextArea(
"The quick brown fox jumps over the lazy dog.");
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane pane = new JScrollPane(textArea);
pane.setPreferredSize(new Dimension(500, 200));
pane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
final JButton button1 = new JButton("Select All");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.selectAll();
// Transfer focus to JTextArea to show the selected
// text.
button1.transferFocusBackward();
}
});
final JButton button2 = new JButton("Get Selected Text");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = textArea.getSelectedText();
System.out.println("Text = " + text);
}
});
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(button1);
buttonPanel.add(button2);
this.add(pane, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}
public static void showFrame() {
JPanel panel = new TextAreaSelectAll();
panel.setOpaque(true);
JFrame frame = new JFrame("JTextArea Demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TextAreaSelectAll.showFrame();
}
});
}
}
And here the screenshot of the code snippet above:
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020