Jump to content
  • Advertisement
Sign in to follow this  
Mazh

java - JTextField and newline

This topic is 3470 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


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

Share this post


Link to post
Share on other sites
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
yes, i've found, but still on a newline, tried many way to get it works, and I'm tried this ain't working.

Share this post


Link to post
Share on other sites
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!