How would you get a JTextArea to know how many lines it had?
Valid lines that is?
1.) It'd be nice to get line numbers to make a notepad document to transfer to several lines when it writes it versus just 1 line.
2.) In case you wanted it to print out all lines until there truly is no more data, note, there could be like spaces in between data and I'm baffled how to deal with that part too.
I'll show the code later. I have it but not with me at the moment.
Re: How would you get a JTextArea to know how many lines it had?
I assume you are talking about the number of rows after text wrapping. Otherwise, if the problem you are having is related to line breaks (\n) not translating over to notepad, you need to use the System.getProperty("line.separator"); for whatever reason. Oddly enough, tab (\t) translates over to notepad, but the line break (\n) doesn't. Who knows.
If your problem is text wrapping, then you are going to hate this. I did it awhile ago, and it is elaborate as shit, but it works. You have to use the AttributedString, LineBreakMeasurer, and TextLayout classes. I'll be surprised if you have ever heard of any of those, I hadn't beforehand.
I have some useful code for you, but there is a little bit that I don't have that you'll have to figure out (I think substring will work, but you'll have to try).
Here is a test program:
public class CheckNumbersProgram
public static void main(String args)
String text = "Java is a programming language originally developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture.";
AttributedString attributedString = new AttributedString(text);
AttributedCharacterIterator attributedCharacterIterator = attributedString.getIterator();
int start = attributedCharacterIterator.getBeginIndex();
int end = attributedCharacterIterator.getEndIndex();
LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(attributedCharacterIterator,new FontRenderContext(null, false, false));
Dimension size = new Dimension(500,10);
float width = (float) size.width;
while(lineBreakMeasurer.getPosition() < end)
TextLayout textLayout = lineBreakMeasurer.nextLayout(width);
The X value of size determines where the lines will be broken. For that reason, you want the X value of size to be equal to the X size of your Text Area. This code will not actually break up the String for you. TextLayout is primarily for graphics (which was my application), not actually giving a String. TextLayout will, however, give you indexes as to where things should start and end. So using those indexes, you can probably substring it into lines.
Hope this helps.