aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Visible column and row count in JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Visible column and row count in JTextArea" Watch "Visible column and row count in JTextArea" New topic
Author

Visible column and row count in JTextArea

Joe carco
Ranch Hand

Joined: Apr 14, 2009
Posts: 82
Hi All,

I've been doing a lot of googling and I've searched many forums, but no luck on this one:

I have a JTextArea with linewrapping = true in a JScrollPane. My JTextarea has 80 columns and 40 rows. I use it to read and display text files of arbitrary size. Because I want to be able to read text files of any size I want to only display the amount of text that fits into the textarea. When I press "key down" or up I want to display the next or previous line respectively. For this to work I need to know exactly how many characters fit into a line and how hany lines of text into the visible area of my JTextArea.
Another factor is the font size for obvious reasons.

If I knew how many characters of a certain font fit into a JTextarea with 80 columns I could initialize a char buffer with a precise amount of fields for reading from the BufferedReader, and if I knew how many lines of text with a certain font fit into my JTextArea with 40 rows I could read just enough text on program start to fill the Textarea with text.

I hope this doesn't sound too confusing and that there is someone here who could maybe help me with my problem
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Is there a specific reason you want to do this? It sounds like whatever you want to achieve won't warrant what you're trying to do.
Joe carco
Ranch Hand

Joined: Apr 14, 2009
Posts: 82
its going to be the next big thing!!!

kidding

its just experimental. I'm a big fan of posix apps like "more" and "less" and I wanted to write a java port of it with a GUI - just for fun.

Maybe JTextArea isn't the right choice of Swing component for this? I would really appreciate any help
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Well, most of what you want to do is already handled by JScrollPane. What do you want to do that isn't already provided by JScrollPane?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Stephan van Hulst wrote:Well, most of what you want to do is already handled by JScrollPane. What do you want to do that isn't already provided by JScrollPane?

No, Joe wants to read the input file lazily. JScrollPane doesn't handle that.


luck, db
There are no new questions, but there may be new answers.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

In that case I can only think of two solutions. The difficult way is as follows:

- Somehow get the FontRenderContext from the JTextArea. Best way to do this would probably be to extend JTextArea and retrieve it from the Graphics2D instance by overriding paintComponent. This has to be done after the Font is set.
- Construct GlyphVectors from the Font, using part of the file input and the FontRenderContext as arguments.
- Determine whether the GlyphVector would fit on the line in the JTextArea, using its visual bounds.
- If so, repeat until it doesn't fit. You now know how much of the input fits on the line.

This is very ugly and error-prone. Better is to use a mono-spaced font and use the amount of columns to determine how much text fits.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

Stephan van Hulst wrote:This is very ugly and error-prone. Better is to use a mono-spaced font and use the amount of columns to determine how much text fits.


And since the application is supposed to be emulating Unix commands, this would be a completely reasonable design choice. In fact using a proportional font would be the choice which is unreasonable.
Joe carco
Ranch Hand

Joined: Apr 14, 2009
Posts: 82
Stephan van Hulst wrote:In that case I can only think of two solutions. The difficult way is as follows:

- Somehow get the FontRenderContext from the JTextArea. Best way to do this would probably be to extend JTextArea and retrieve it from the Graphics2D instance by overriding paintComponent. This has to be done after the Font is set.
- Construct GlyphVectors from the Font, using part of the file input and the FontRenderContext as arguments.
- Determine whether the GlyphVector would fit on the line in the JTextArea, using its visual bounds.
- If so, repeat until it doesn't fit. You now know how much of the input fits on the line.

This is very ugly and error-prone. Better is to use a mono-spaced font and use the amount of columns to determine how much text fits.


I'm already using a mono-spaced font.
I wanted to make it a bit more sophisticated: I want to be able to resize the window i.e the JTextArea in the JScrollPane, therefore I will have to recalculate the number of mono-spaced characters that can fit into the current visible area of my JTextArea. I guess I'll give Stephan's difficult way a shot.
Joe carco
Ranch Hand

Joined: Apr 14, 2009
Posts: 82
Got this one figured out, thanks Stephan for the idea about extending JTextArea that did the trick. It was much easier than Stephan initially suggested however.

The following code in JTextArea pretty accurately calculates the number of rows and columns (chars) that fit into the visible area with linewrap set to true, even after resizing the visible area:

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Nice job man.

My was was more difficult because it will calculate the width for every character, whereas you calculate the width for the widest character.
You have a fine solution.

Now, if your pager is read-only, what you could do is replace the existing KeyListener for the JTextArea, with one that will scroll the view up or down a line, regardless of where the caret is.
 
wood burning stoves
 
subject: Visible column and row count in JTextArea