SWT Java Tutorial

(From SWT API)
The preferred size of a control is the size that it would best be displayed at.
The width hint and height hint arguments allow the caller to ask a control
questions such as "Given a particular width, how high does the control need
to be to show all of the contents?" To indicate that the caller does not wish
to constrain a particular dimension, the constant SWT.DEFAULT is passed for
the hint.

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GetPreferredSize {
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Button button = new Button(shell, SWT.PUSH | SWT.LEFT);
    button.setText("Button");
    System.out.println("toControl: " + button.toControl(100, 200));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}