This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make a button a fixed size?  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a button that, when pressed, changes text. When the text changes the size of the button changes. How do I prevent this from happening?

SSCCE below:


I don't care what size the button is (well, anything reasonable), I just don't want it changing size when I change the text.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a panel with a CardLayout. Then create two buttons and add each button to the CardLayout. The size of the panel will now be the size of the largest button. So when you click on a button you will need to swap buttons.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. The real code has more than 1 button, using the card manager looks like a huge hassle. I solved the problem by calling setSize(), setPreferredSize(), and setMaximumSize(), all with the same arguments, in ActionPerformed. You have to call all 3, call any 1 or 2 and the button resizes.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will work, until you have some different text that no longer fits in the dimensions you've chosen.

It's depressing to see good advice from an expert like Rob willfully ignored in favor of a quick fix that can come back to bite you later.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, the program was to scratch an itch. That itch got scratched months ago, but I've spent 90% of my time in this program dorking with Swing and, quite frankly, I'm sick of it.

I'm not selling it, nobody else will ever see it (unless you want a copy, then drop me a line), and I'm done playing with it.

Not to demean Rob's contribution, I do appreciate it. But as I looked into using card I just had the feeling I was sinking deeper in the GUI mess of Swing, which is something I have no interest in.

 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really want to change the button's text (as opposed to managing two buttons) there are ways it can be done. Two come to mind:

1) Use a layout manager that doesn't respect preferred sizes. You could replace your BoxLayout with GridLayout, or use the NORTH or SOUTH regions of BorderLayout. (downside: This approach may stretch your button wider than you would like.)

2) Call myButton.setPreferredSize() sometime before frame.pack(). (downside: It can be difficult to choose a size that works well with all possible LnFs.) In this particular case, since you're starting with a longer word and changing to a shorter one, something like myButton.setPreferredSize(myButton.getPreferredSize()) might work. Or myButton.setPreferredSize(myButton.getSize()) inside an InvokeLater.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll give that a shot. I did spend some time on it yesterday, got nowhere with how I wanted the GUI to look using Card.

I may be frustrated, but I've been doing this too long to let something like mis-sized buttons get the best of me
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!