The following example shows you how to set the property of the JTextArea
so that it cannot be edited or modified. To make the JTextArea
not editable call the setEditable()
method and pass a false
value as the parameter.
package org.kodejava.swing;
import javax.swing.*;
import java.awt.*;
public class TextAreaNotEditable extends JPanel {
public TextAreaNotEditable() {
initializeUI();
}
public static void showFrame() {
JPanel panel = new TextAreaNotEditable();
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(TextAreaNotEditable::showFrame);
}
private void initializeUI() {
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(500, 200));
JTextArea textArea = new JTextArea(5, 50);
textArea.setText("The quick brown fox jumps over the lazy dog.");
// By default, the JTextArea is editable, calling
// setEditable(false) produce uneditable JTextArea.
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
this.add(scrollPane, BorderLayout.CENTER);
}
}
The output of the code snippet is:
Latest posts by Wayan (see all)
- How do I split large excel file into multiple smaller files? - April 15, 2023
- How do I get the number of processors available to the JVM? - March 29, 2023
- How do I show Spring transaction in log / console? - March 29, 2023
setEnabled(false)
will make the JTextArea non-editable.