button size/insets

I have a button with only one character as text. I want this button as small as possible. How can I change its insets ?

- button.getInsets().set(0, 0, 0, 0) doesn't seem to work
- if I do button.setBorder(null) - I get the smallest possible size which is what I want, but I lose, of course, the button's nice look

Thanks
hapciuAsked:
Who is Participating?
 
TimYatesConnect With a Mentor Commented:
Insets margins = new Insets( 0, 0, 0, 0 ) ;
JButton b = new JButton("I");
b.setMargin( margins ) ;

?
0
 
CEHJCommented:
Use a smaller font ?
0
 
objectsCommented:
you could use a narrower border but that doesn't save you much.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
hapciuAuthor Commented:
exactly. how do i change the border's size/insets without really knowing the border's type ? well button.getInsets() or button.getBorder().getBorderInsets() and change those you say... but it don't work.

Just try this:
JButton b = new JButton("I");
// add the button to your favourite container and see how excessively wide it is :)
System.out.println(b.getInsets());
b.getInsets().left = 0;
b.getInsets().right = 0;
System.out.println(b.getInsets());

... nothin


 And I'd like this to work on all L&Fs :)
0
 
objectsCommented:
you don't need to know the border size, you only need to set it to whatever border you want.
0
 
hapciuAuthor Commented:

I found out myself just now - one trick is to extend Jbutton and overwrite getInsets() to return what you want - in my case new Insets(2, 2, 2, 2)

TimYates' solution is shorter though - I should've searched the Javadoc more careful (now where's that "I'm ashamed" emoticon ?)
0
 
TimYatesCommented:
:-)

hehe, good luck!!

Tim
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.