Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing: checkbox spacing as compared to labels

How do you remove/reduce the spacing between checkboxes with MigLayout?

alt text

import java.util.Arrays;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class CheckboxSpacing {
    public static void main(String[] args) {
        JFrame frame = new JFrame("checkbox spacing");
        JPanel panel = new JPanel();
        frame.setContentPane(panel);
        panel.setLayout(new MigLayout());
        for (String verb : Arrays.asList("see","hear","speak"))
        {
            JCheckBox cb = new JCheckBox(verb+" no evil");
            panel.add(cb, "wrap");
        }

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

NOTE: I'm wondering maybe if the MigLayout part of this question is a red herring. If I change my program to the following, I get these dialogs:

alt textalt text

You will note the difference in spacing between labels and checkboxes. How can I get rid of the excess spacing for checkboxes?

import java.awt.Component;
import java.util.Arrays;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class CheckboxSpacing {
    enum WhichGUI { LABEL {
        @Override
        public Component createComponent(String text) {
            return new JLabel(text);
        }
    }, CHECKBOX {
        @Override
        public Component createComponent(String text) {
            return new JCheckBox(text);
        }
    };
        abstract public Component createComponent(String text); 
    }
    public static void main(String[] args) {
        doit(WhichGUI.LABEL);
        doit(WhichGUI.CHECKBOX);
    }
    private static void doit(WhichGUI which) {
        JFrame frame = new JFrame("spacing: "+which);
        JPanel panel = new JPanel();
        frame.setContentPane(panel);
        panel.setLayout(new MigLayout(
                  "",  //layout
                  "[]", //column
                  "0"  //row
            ));
        for (String verb : Arrays.asList("see","hear","speak"))
        {
            Component c = which.createComponent(
                      verb+" no evil (Jabberwocky! Çgpq)");
            panel.add(c, "wrap 0");
        }

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
like image 621
Jason S Avatar asked Jan 12 '11 15:01

Jason S


2 Answers

The difference is in the preferred size of each component.

String text = "Hear no evil";
JLabel label = new JLabel(text);
System.out.println( label.getPreferredSize() );
System.out.println( label.getInsets() );

JCheckBox checkBox = new JCheckBox(text);
System.out.println( checkBox.getPreferredSize() );
System.out.println( checkBox.getInsets() );

A check box has non-zero insets. This would be from the border. So you should be able to either:

  1. set the border of the check box to null
  2. add an EmptyBorder to the label
like image 85
camickr Avatar answered Nov 15 '22 04:11

camickr


There are a number of ways to accomplish this and they are well specified in this document.

You can specify the spacing either between the grid or the components. You probably want to do something like this:

     new MigLayout(
              "",  //layout
              "[]", //column
              "5"  //row
        );

And that should set the gaps. You will have to play with the numbers until you get what you want.

A second option (and I think you have more control this way is to do this:

  panel.add(cb, "wrap 0");

Where the 0 specifies the gap between rows.

like image 43
Vincent Ramdhanie Avatar answered Nov 15 '22 04:11

Vincent Ramdhanie