views:

87

answers:

3

When i create a JCheckBox in my Swing application i leave some extra space after its label, so if the JCheckBox label is for example 100 pixels width, i make the JCheckBox 120 pixels for safety.

The problem as at runtime, it's not nice that a user can click on the empty space after the JCheckBox label and it can be actually clicked, like this :

http://imgur.com/3eMUG.png

I wonder if there is a way to resize the JCheckBox at runtime to exactly fit the text inside it, depending on the font type/size used ?

This seems fancy a bit, but i like to make things look perfect :)

Edit - More details :

I use FormLayout, and I define the JCheckBox like this :

CellConstraints con = new CellConstraints();
contentPane.add(checkBox1, con.xywh(3, 13, 10, 1));
+1  A: 

to exactly fit the text inside it, depending on the font type/size used

This is the default preferred size of JCheckBox; just don't change it.

trashgod
+1  A: 

@trashgod is correct. If you want padding, aligning, or spacing to aesthetic purposes, you should use the layout managers to achieve this and not mess with the components themselves. Pretty much any layout you want to achieve can be done this way. You may need to use GridBagLayout, which can be a pain, but it's the most flexible.

davetron5000
A: 

Okay ... Thanks all for your answers. I have found a suitable solution while using FormLayout :

contentPane.add(checkBox1, con.xywh(3, 13, 10, 1, CellConstraints.LEFT, CellConstraints.DEFAULT));
Brad