JRadioButton border / padding / insets / margin ... whatever you call

In my Java application, I am trying to create a very simple form with a label and a set of controls on each line of the form. Imagine something like this rough ASCII diagram:

  Result 1: (*) pass () fail
    Result 2: () pass (*) fail
 Error Count: [10______]
 Explanation: [Operator overload___]

Annoyingly, JRadioButton does not line up with the rest of the controls, as they have a large number of pads around, pushing them to the right a couple of pixels and adding a lot of space between the lines. I get something like this:

  Result 1: (*) pass () fail

    Result 2: () pass (*) fail

 Error Count: [10______]
 Explanation: [Operator overload___]

How can I make the switches stop having so much free space so that they can work well with everything else? If that matters, it's using GTK L & F; I have not tried running the program under Windows.

+4
source share
3 answers

There seem to be two culprits:

  • The mini JPanel , which contains two switches, has a FlowLayout , which by default adds 5 pixels of padding around each component.

  • Running radioButton.setBorder(null) removes the extra pixel around the buttons. It also screws up the dashed line drawn around them when they have focus.

+2
source

Use the GridBagLayout and make sure to snap the cells (each label and checkbox have their own cell) left or right as necessary. Labels would be correctly justified, flags would be justified on the left.

Since manually configuring GridBagLayouts is a hassle, I recommend using the NetBeans GUI builder and customizing them using the customize graphical tool.

0
source

Another solution might be to change the field (setMargin method). That should do the job. The only drawback is that the fields / inserts will be different for different LAFs.

0
source

All Articles