-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?

closed as off-topic by gnat, Ixrec, James McLeod, user22815, Jörg W Mittag Mar 6 '16 at 17:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for assistance in writing or debugging existing code are off-topic on Programmers. These questions can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question itself. See How To Create a Minimal, Complete, and Verifiable Example." – Ixrec, James McLeod, Community
If this question can be reworded to fit the rules in the help center, please edit the question.

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.