How can I get a single line of text of a textarea?
So like if I had
"Hello
Mellow
Yellow"
Then line 1 would be Hello and line 2 would be Mellow ect.
How can I get that?
Something like:

JTextArea area = new JTextArea();

System.out.print(area.getLineOfText());

Thanks.

That's not going to be a simple one. The contents of a text area are stored as a single line, with wrapping provided by the component, so there isn't a method that'll just return an arbitrary line.

I would probably set the font to a monospace and see if you can find some correlation between the result of "getcolumnwidth" and the length of a line. If there is such a correlation, you'd pretty much have it, just get the substring (0,getColumnWidth()) of the textarea's getText(). You'd just need to remember to backtrack from end of line to the first word break preceding end of line, otherwise you'd get "Hello Mel" or something like that.

Comments
Verry good way to solve this problem

I'm trying to make it save to a text file from user input and I don't think a user would know that they need to enter a monospace.
Do text areas recognize line breaks?
When I save all the text in the text area into a text file, I print(textarea.getText()) into it which doesn't recognise line breaks.

class savefile implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				final Formatter SF;
				String saves = null;
				if(saves == null){
					int ReturnVal = savef.showSaveDialog(null);
					if(ReturnVal == savef.APPROVE_OPTION){
						File sf = savef.getSelectedFile();
						saves = sf.getAbsolutePath();
						try {
							SF = new Formatter(saves);
							FileWriter fstream = new FileWriter(saves);
							BufferedWriter write = new BufferedWriter(fstream);
							PrintWriter print = new PrintWriter(write);
							[B]print.println(textar.getText());[/B]
							print.close();
							write.close();
							SF.close();
						} catch (FileNotFoundException e) {
							
							JOptionPane.showMessageDialog(frame,e.getStackTrace());
						} catch (IOException e) {
							JOptionPane.showMessageDialog(frame,e.getStackTrace());
						}
					}
				}

I think the bold bit is the most important. It prints the text but doesn't recognize line breaks. Do you know something that will?

A text area size is based on the number of rows for height and the number of letters for width. So as long as you know the width of the text area you can divide up the text string into lines.

You have complete control over the type of text that is entered in so all it takes is for you to set the font type to monospace.

Where are you getting the input from? (ie. a java program, a html page, another program.)

Also are individual words staying on the same line or if they are at the end of a line does it split the word between this line and the next?

I'm getting input from the user on a Java program.
The width of my text area depends on the width of the window which the user can adjust...
How can I set the font type to a monospace?
Well, you keep typing until it reaches the end. If you keep typing after that, it goes off the screen.

The only way to get one line of a text area that is adjustable is to get the size of it when the user submits the text which I don't know how to do. Jon did tell you how to get the text area width though.

"just get the substring (0,getColumnWidth()) of the textarea's getText(). You'd just need to remember to backtrack from end of line to the first word break preceding end of line, otherwise you'd get "Hello Mel" or something like that. "

The text property on the other hand I can do.

here is the code:

[B]textarea[/B].setFont(new font("monospaced", Font.PLAIN, [B]12[/B]));

The items in bold you can change the first one needs to be the name of your textarea the second one I think is for the size of the letters.

I am not sure if I did this right so please correct me if I am wrong.

Edited 5 Years Ago by DarkLightning7: n/a

Comments
Looks about right to me. Good answer.

Actually, Sorry guys but for some reason, it recognizes line breaks! And I don't even think it should :S
Here is my code by the way:

class savefile implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Formatter SF;
				String saves = null;
				if(saves == null){
					int ReturnVal = savef.showSaveDialog(null);
					if(ReturnVal == savef.APPROVE_OPTION){
						File sf = savef.getSelectedFile();
						saves = sf.getAbsolutePath();
						try {
							SF = new Formatter(saves);
							FileWriter fstream = new FileWriter(saves);
							BufferedWriter write = new BufferedWriter(fstream);
							PrintWriter print = new PrintWriter(write);
							print.println(textar.getText());
							print.close();
							write.close();
							SF.close();
						} catch (FileNotFoundException e) {
							
							JOptionPane.showMessageDialog(frame,e.getStackTrace());
						} catch (IOException e) {
							JOptionPane.showMessageDialog(frame,e.getStackTrace());
						}
					}
				}else{
					try {
						SF = new Formatter(saves);
						FileWriter fstream = new FileWriter(saves);
						BufferedWriter write = new BufferedWriter(fstream);
						PrintWriter print = new PrintWriter(write);
						print.println(textar.getText());
						print.close();
						write.close();
						SF.close();
					} catch (FileNotFoundException e) {
						JOptionPane.showMessageDialog(frame, e.getStackTrace());
					} catch (IOException e) {
						JOptionPane.showMessageDialog(frame,e.getStackTrace());
					}
				}
				if(saves != null){
					try {
						SF = new Formatter(saves);
						FileWriter fstream = new FileWriter(saves);
						BufferedWriter write = new BufferedWriter(fstream);
						PrintWriter print = new PrintWriter(write);
						print.println(textar.getText());
						print.close();
						write.close();
						SF.close();
					} catch (FileNotFoundException e) {
						JOptionPane.showMessageDialog(frame, e.getStackTrace());
					} catch (IOException e) {
						JOptionPane.showMessageDialog(frame,e.getStackTrace());
					}
				}
				}
				
			}
This question has already been answered. Start a new discussion instead.