Java Swing How to - Set the Gap Size Between the Label and Icon in a JButton








Question

We would like to know how to set the Gap Size Between the Label and Icon in a JButton.

Answer

import javax.swing.JButton;
import javax.swing.JOptionPane;
public class Main {
  public static void main(final String args[]) {
    JButton button = new JButton();
//from w  w w.  j  a va  2 s  .c o m
    // Get gap size; default is 4
    int gapSize = button.getIconTextGap();

    // Set gap size
    button.setIconTextGap(8);
    
    JOptionPane.showMessageDialog(null, button);
  }
}