How to adjust the width of JButton ?

Dera Friends:I met problem,I have a batch of JButtons, but eah button has only few words in text, such as ABC, etc, but JButton is too fat or too wide, I really hope to reduce its width to fit its text.How to do it?Thankssunny
[268 byte] By [sunnymanmana] at [2007-11-26 19:36:27]
# 1
Use the setMargin() method to set the insets to 0.
camickra at 2007-7-9 22:12:21 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks, I use Button.setMargin(new Insets(0, 0, 0, 0));but stll so wide, any other way?Regards
sunnymanmana at 2007-7-9 22:12:21 > top of Java-index,Desktop,Core GUI APIs...
# 3

Usually the buttons get sized by the Layout used.

which layout are you using?

if u r using JPanel the default is FlowLayout,

in this case Flowlayout doesn't control the width just the placement.

so before u add the button 2 the panel use

button.setPreferredSize(new Dimension(100,100));

or

button.setSize(100,100);

avdzm

avdzma at 2007-7-9 22:12:21 > top of Java-index,Desktop,Core GUI APIs...
# 4
use a holdingPanel (for the additional space), or use a layout manager that doesn't 'fill'if you are unsure, post a small demo program - just a frame/panel/3 buttons, and we'll add the change
Michael_Dunna at 2007-7-9 22:12:21 > top of Java-index,Desktop,Core GUI APIs...
# 5
If you're placing 3 buttons on vertical alignment using border layout, thus having the auto expand problem, you can try put each of the button into a JPanel with flow layout, then place the 3 JPanel's into the final container using border layout.
Icycoola at 2007-7-9 22:12:21 > top of Java-index,Desktop,Core GUI APIs...
# 6
Dear friends,I used gridLayout,your code button.setPreferredSize(new Dimension(100,100));or button.setSize(100,100);can only change button's height, but not width, why?But I hope to chnage width, Thanks
sunnymanmana at 2007-7-9 22:12:21 > top of Java-index,Desktop,Core GUI APIs...
# 7

> can only change button's height, but not width, why?

Because you have a coding problem. How many times do you need to be asked to post a SSCCE?

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

Every question you post should have a SSCCE, so we don't waste time quessing what you are doing. 95% of the time when you create the SSCCE you will find your problem.

camickra at 2007-7-9 22:12:21 > top of Java-index,Desktop,Core GUI APIs...
# 8

The part of "why" is actually simple.

Some layout managers will auto expand component width/height to fill the container, such as borderlayout, or grid layout in your case.

The part "how to solve it" is the challenge.

Apparently the way to "hack" this behaviour is to put your buttons into a JPanel with layout manager that wont auto expand, such as flow layout. You need to tell me how you want your button arranged for further help =)

Icycoola at 2007-7-9 22:12:21 > top of Java-index,Desktop,Core GUI APIs...