java - JTextField and newline

Started by
5 comments, last by Mazh 15 years, 4 months ago
Hello there, Basically, I can't understand why I'm not getting a new line with this code: I've looked everywhere and this should normally works ... Maybe it's because I use arrays, but I can't really understand as the output in mywindows is on an only vertical line.
[source langage=java]
JTextField label[]=		new JTextField[5];

final String newline ="\n";
		
		for(int i=0;i<5;i++){
			line=input.readLine();
			label=new JTextField(line+newline);
			label.setForeground(Color.red);
			label.setEditable(false);
			pLabel.add(label);
		}

Thanks to everyone that can help with this small problem ^^
Advertisement
Text fields are one-liners. What you want, to have the option of showing multiple lines, is a text area (JTextArea).

Assuming I understood the problem correctly.
Tried with JTextArea, but this is still on the same line...can't make it works as I want :/

[Edited by - Mazh on December 15, 2008 8:09:06 PM]
JTextArea has setEditable() method it inherits from JTextComponent...
yes, i've found, but still on a newline, tried many way to get it works, and I'm tried this ain't working.
You might want to keep your tests. Later, it helps figure out what you missed and need to focus on later. Yay unit testing.

Here's the test I wrote:

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Test extends JFrame {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test();
}

public JTextField textField;
public JTextArea textArea;
public Test()
{
super();
setBounds(0,0,800,500);
setupTextField();
setupTextArea();
setLayout(null);
setVisible(true);
}

public void setupTextField()
{
String message1 = "my message1";
String message2 = "message2 here";
String newline = "\n\r";

textField = new JTextField(message1+newline+message2);
textField.setBounds(0,0,500,160);
add(textField);
}

public void setupTextArea()
{
String message1 = "my message1";
String message2 = "message2 here";
String newline = "\n\r";

textArea = new JTextArea(message1+newline+message2);
textArea.setBounds(0,165,500,200);
add(textArea);
}
}

It doesnt exit correctly, but demonstrates the concepts. You dont need the returns (\r) but it's a habit. A google search gave me: http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html Sun does a very good job of covering basic component functionality and some beyond.
yeah, I've browsed this website before, and when I test it out on a different project, it works too except when I start using arrays, what's so different so it won't work with it? I don't know and I wonder why :/ I've tried some researches without a real success at the moment.

This topic is closed to new replies.

Advertisement