File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JButton and setSize() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton and setSize()" Watch "JButton and setSize()" New topic

JButton and setSize()

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
I want to set up my JButton so that there is minimum marging between the name of the button and it's borders:
| M M |
|<--->Button <---->|
| |
I tried doing:
JButton button = new JButton();
But it has no affect on how the JButton looks. What should I do?

All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
eric moon
Ranch Hand

Joined: Nov 26, 2000
Posts: 133
You have to call setPreferredSize() to get the button to resize. You may also need to call setMinimumSize() depending on the layout manager you are using.

<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>"Those who cast the votes decide nothing. Those who count the<BR>votes decide<BR>everything." <BR> -Joseph Stalin<HR></BLOCKQUOTE>
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Use the setMargin method. The setPreferredSize() deals with the entire button not the margin between the edge and the text. The setMargin method takes an Insets as input.
Brian Dienes

Joined: Oct 21, 2005
Posts: 3
Thanks. setMargin works great for me!
I agree. Here's the link:
subject: JButton and setSize()
It's not a secret anymore!