Sign in to follow this  
Mazh

java - JTextField and newline

Recommended Posts

Mazh    138
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[i]=new JTextField(line+newline);
			label[i].setForeground(Color.red);
			label[i].setEditable(false);
			pLabel.add(label[i]);
		}

Thanks to everyone that can help with this small problem ^^

Share this post


Link to post
Share on other sites
Lajnold    226
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.

Share this post


Link to post
Share on other sites
Mazh    138
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]

Share this post


Link to post
Share on other sites
Jack9    102
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.

Share this post


Link to post
Share on other sites
Mazh    138
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this