• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to make a button a fixed size?

 
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3244
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic