Следующий код делает трюк:
final int style = OS.GetWindowLong(button.handle, OS.GWL_STYLE);
OS.SetWindowLong(button.handle, OS.GWL_STYLE, style | OS.BS_MULTILINE);
button.setText("line 1\nline 2");
Вы просто импортируете org.eclipse.swt.internal.win32.OS и все. Конечно, этот класс и поле handle внутри кнопки не являются частью SWT API, поэтому ваш код больше не является переносимым. Но функции определены в Windows API, поэтому вам не нужно слишком беспокоиться, они изменятся в будущих версиях SWT.
Имейте в виду, что после этого изменения computeSize () больше не работает.
Редактировать: полный класс, где я забочусь о computeSize () и стиле GWL
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
public class MultilineButton extends Button {
public MultilineButton(Composite parent, int style) {
super(parent, style);
final int gwlStyle = OS.GetWindowLong(this.handle, OS.GWL_STYLE);
OS.SetWindowLong(this.handle, OS.GWL_STYLE, gwlStyle | OS.BS_MULTILINE);
}
@Override
protected void checkSubclass() {
}
@Override
public Point computeSize(int wHint, int hHint, boolean changed) {
final Point size = super.computeSize(wHint, hHint, changed);
final GC gc = new GC(this);
final String multiLineText = this.getText();
final Point multiLineTextSize = gc.textExtent(multiLineText, SWT.DRAW_DELIMITER);
final String flatText = multiLineText.replace('\n', ' ');
final Point flatTextSize = gc.textExtent(flatText);
gc.dispose();
size.x -= flatTextSize.x - multiLineTextSize.x;
size.y += multiLineTextSize.y - flatTextSize.y;
return size;
}
}