-1

I'm writing this code here:

import java.awt.FlowLayout;

import javax.swing.JFrame;

import javax.swing.JTextArea;

import javax.swing.UIManager;

public class FirstJavaClass {
public static void main(String[] args) {
try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}

JFrame f = new JFrame("Example");
f.getContentPane().setLayout(new FlowLayout());
f.getContentPane().add(new JTextArea("Text Area\nText Area", 18,18));

f.pack();
f.setVisible(true); 

   }
}

When I run it I get a window with the "text area" printed on but I can type on it. How can I prevent the user from modifying whatever message I wrote there?

1

You could use JLabel as output -only text field.

Or if you insist on using JTextArea, you can set the editable -property as false.

    JTextArea area = new JTextArea("Text Area\nText Area", 18,18);
    area.setEditable(false);
    f.getContentPane().add(area);

Not the answer you're looking for? Browse other questions tagged or ask your own question.