Problem with Multi-Line Jtextfield

I have a JTextField and need to have 3 lines in it, such as these:

HI
MY name
is mehdi

How can I set the text in a JTextField? I tried \n, but it didn't work.

3 Answers

1 : JTextField does not support multiline. what you want is a JTextArea

2 : Newlines aren't the same on all systems, you might have to use "\r\n" if you are running Windows. Since Java 7, there is System.LineSeparator to solve this issue.

(see the Oracle documentation)

7

You can't get multi-line behavior on single-line JTextField,

use JTextArea instead

info:

JTextArea taText = new JTextArea();
taText.setText("\tHI\nMY name\nis Raku");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest