This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Making sure Text fits within JButton... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Making sure Text fits within JButton..." Watch "Making sure Text fits within JButton..." New topic
Author

Making sure Text fits within JButton...

Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
I have been trying to make the label of JButton to be displayed. But all that shows up is "...". This is because the display area of JButton is small. So I tried reducing the Font size of JButton to a minimum. What happens is that the size of "..." gets reduced!!! Text is still not displayed! And its not a long string, its just a single number.



With Best Regards,
Shyam Prasad Murarka
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Post a minimal example program demonstrating your problem.

Don't explciitly set the preferred size of a button.
Are you pack()ing your JFrame? Then components should be receiving adequate
dimensions unless a layout manager is dropping the ball.


There is no emoticon for what I am feeling!
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
Whatever code was relevant to the problem I have posted it. The rest of the code is many lines long. What I am trying is to make a minesweeper game. I have finsihed making it. And its working. But I have to improve the GUI. For that I need to maintain the height and width of each box. Is there any other way other than using setPreferrdSize()??
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
works OK like this

Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
Michael, thanks for your sample. It made me realize something. Your code worked perfectly.
But I wanted the button size to be (15,15) so I changed it. But the double digits number did not show up this time.
Then I commented the line that sets the Margin. This time the single digits also did not show up!!
So i put back the setMargin() line and the single digits showed up. That's what I wanted. Because in Minesweeper the numbers will never be more than 8.
So its working, BUT I still don't understand why its working??

What I estimated is that maybe JButton by default sets a margin greater than 0 resultantly eating away the precious display area!!
And by setting it to 0 in the above code, the text gets more space to display iself. So atleast the single digits are being displayed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Making sure Text fits within JButton...
 
Similar Threads
Customizing JToolTip
MenuItem font setting
JEditorPane with CSS font size attributes
Selecting text
using font created by createFont