Question

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

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

Annoyingly the JRadioButtons don't line up with the rest of the controls as they have a large amount of padding all around, pushing them to the right a couple of pixels and adding a lot of space between lines. I end up with something like this:

   Result 1:   (*) pass  ( ) fail

   Result 2:   ( ) pass  (*) fail

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

How can I get the radio buttons to stop having so much empty space so they can line up nicely with everything else? If it matters this is using the GTK L&F; I haven't tried running the program under Windows.

Was it helpful?

Solution

It looks like there are two culprits:

  1. The mini-JPanel containing the two radio buttons has a FlowLayout which defaults to adding 5 pixels of padding around each component.

  2. Doing radioButton.setBorder(null) eliminates another pixel's worth of space around the buttons. It also screws up the dotted line drawn around them when they have focus, though.

OTHER TIPS

Use a GridBagLayout, and make sure to anchor cells (each label and checkbox would have its own cell) towards the left or towards the right as needed. The labels would be right-justified, the checkboxes would be left-justified.

Since customizing GridBagLayouts by hand is a hassle, I recommend using the NetBeans GUI builder and adjusting them using its graphical "customize" tool.

Another solution can be to change margin (radionbuttons's setMargin method). This should do the job. The only downside is that margins/insets will be different for different LAFs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top