This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

changing font seems to change the whole area of my JTextArea  RSS feed

 
Marius Semeon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find the relationship between preferred size of JComponents and restrictions imposed upon them by their containers and their containers layout managers quite confusing, but I am starting to get the hang of when it is appropriate to use different managers to arrange things as desired. But now for some reason, changing font size of the font in JTextArea changes the whole size of the area, even when there is no text in it!? can't figure out the reason for this.. I have the following:

In case it is at all relevant, I put the textArea into a JScrollPane, added the JScrollPane to a JPanel and then added the JPanel to the content pane of the JFrame (to the CENTER). Layout managers are their defaults (BL for JFrame, FL for JPanel).

Thanks for any help
 
Carey Brown
Saloon Keeper
Posts: 3237
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create your JPanel set its size. Meaning
see if that helps
 
Marius Semeon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but that didn't seem to work. The font size of the JTextArea still seems to dictate how big the textarea is
 
Carey Brown
Saloon Keeper
Posts: 3237
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, try that on your JTextArea instead.
 
Carey Brown
Saloon Keeper
Posts: 3237
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the part of your code where you are creating the JPanel, JScrollPanel, and JTextArea?
 
Rob Camick
Ranch Foreman
Posts: 2818
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But now for some reason, changing font size of the font in JTextArea changes the whole size of the area, even when there is no text in it!? can't figure out the reason for this


That is correct. The preferred size calculation is based on the font size and the number of rows and columns you want.

So when you use:



You are saying that 25 rows and 45 columns of the text area should be visible on the screen (before scrolling).

So with a larger Font the text area will be a larger size.
 
Marius Semeon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Can you post the part of your code where you are creating the JPanel, JScrollPanel, and JTextArea?


Yes, here it is. Sorry late reply i've been very ill. I tried setting preferred/max/minSize on the JTextArea and it didn't effect it, but I guess it's because it was inside a JScrollPane so it's the outer container that dictates it. When I set the sizes on the JScrollPane it all worked fine. Sorry this is a really basic question, i just fine the whole layout aspect and sizing confusing in java.

 
Campbell Ritchie
Marshal
Posts: 56193
171
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a long time since I tried it, but it used to be possible to use HTML tags on a Component; I think a JLabel. Since the tags can include font size, that might allow you different font sizes for the same size component. Of course, you can't allow the user to enter text on a JLabel as you can on a text component. As RC suggests, you may be stuck with different sizes for different fonts.
 
Brian Cole
Author
Ranch Hand
Posts: 954
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Semeon wrote:I tried setting preferred/max/minSize on the JTextArea and it didn't effect it, but I guess it's because it was inside a JScrollPane so it's the outer container that dictates it. When I set the sizes on the JScrollPane it all worked fine.


What a JScrollPane does is allow you to put a large object inside something (the JScrollPane's JViewport) that is (or at least can be) smaller in size. So the JTextArea itself remains unconstrained, and it's not quite correct to say that the JScrollPane is constraining it.

[note: No big deal, but Dimenension should be corrected to Dimension in your code.]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!