Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making sure Text fits within JButton...

 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
works OK like this

 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic