• Post Reply Bookmark Topic Watch Topic
  • New Topic

know if text fits JTextArea  RSS feed

 
Anna Amatus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Dear all,

I have a JTextArea that the user car resize. Now, I want to check if the JTextArea is displaying all the text in order to change the border color. The JTextArea line wrap is set to true.
Any suggestions on how to do this?

Thanks in advance,
anna.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anna Amatus wrote:I have a JTextArea that the user car resize. Now, I want to check if the JTextArea is displaying all the text in order to change the border color. The JTextArea line wrap is set to true.
Any suggestions on how to do this?

I'm not quite sure what you mean. Do you want to change the colour whenever someone goes beyond the end, or just when it gets resized?

Either way, this strikes me as something you could probably leave till later. Get your screen working before you worry about "bells and whistles" like this.

Mind you, I'm no GUI expert; just an old mucker programmer.

Winston
 
Anna Amatus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Winston,

The JTextArea is not editable. It is filled with data coming from elsewhere. There are many of these JTextArea in the panel with different information. For visualization reasons, initially they are all the same size. Some might match the text length but in some cases the text is too long and the user can resize the JTextArea to fit it. Since there are many I would like the user to see easily in which cases the text is truncated. Apart from this detail the window is already working fine.

Thanks,
Anna.

 
Anuj Sharma R
Greenhorn
Posts: 28
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anna,

Is it your requirement to truncate it? Couldn't you just make the text area scrollable retaining the size of the text area?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have set the line wrap and wrap style word to true, then the text area will automatically break your long text into multiple lines based on the available width. you should also be wrapping up the text area inside a scroll pane. Based on this, could you rephrase what your requirement is?
 
Anna Amatus
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have tried already to put in a scrollapane. It works fine but the scrollbars take too much space and looks ugly. Consider that the textareas have to be pretty small.
What I wolud like to do is:
when I fill the textarea (that is initially is set to a dimension of (70, 30)) check if the text fits within. If it fits leave the border black, otherwise put a red border.



 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out SwingUtilities#computeStringWidth()
I hope you realize, without the scroll bars/pane, the user cannot read the full text if it is bigger than the display area (70,30)
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you can do is use the JTextArea#modelToView() method get the coördinates (Rectangle) of the last line in the JTextArea (visible or not). You can use the length property of JTextArea#getDocument() as the position argument.
Using FontMetrics JTextArea#getFontMetrics() you can calculate how many lines of text will fit - fully visible - in the fixed height JTextArea (also take line-spacing into account), and to which y-coördinate that translates.
Based on that information if the y coördinate of the Rectangle exceeds the maximum y-coördinate you calculated, render the border.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!