Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How would you get a JTextArea to know how many lines it had?

  1. #1
    Join Date
    May 2010
    North Central Illinois
    My Mood
    Thanked 112 Times in 110 Posts

    Smile 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.

  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Thanked 329 Times in 305 Posts

    Default 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:
    import java.awt.font.*;
    import java.text.*;
    import java.awt.Dimension;
    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;
    		int count=0;
    		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.

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    javapenguin (January 20th, 2011)

Similar Threads

  1. Expanding JTextArea
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 27th, 2010, 10:15 PM
  2. Drawing Rectangles and Lines
    By andreizeus in forum AWT / Java Swing
    Replies: 21
    Last Post: October 28th, 2010, 12:59 PM
  3. My lines won't work!!!
    By The Mewzytion in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 2nd, 2010, 10:24 AM
  4. [SOLVED] reading only certain lines from a .txt file
    By straw in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 7th, 2010, 07:49 PM
  5. [SOLVED] Supressing empty lines and does not do until the end! Why ?
    By lumpy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2010, 07:38 AM