File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton and setSize()" Watch "JButton and setSize()" New topic
Author

JButton and setSize()

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
Hi,
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();
button.setSize(2,2);
But it has no affect on how the JButton looks. What should I do?
thanks,
Alex


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
Greenhorn

Joined: Oct 21, 2005
Posts: 3
Thanks. setMargin works great for me!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JButton and setSize()
 
Similar Threads
System.exit(0) applet
What is MVC
Cant include picture in JButton
Cant include picture in JButton
java code for round rectangle java swing button