Preventing a SWT window from being resized horizontally
Is there a STYLE bit that I could apply to my shell/dialog to prevent horizontal resizing? I would like the window to allow vertica开发者_StackOverflow中文版l resize only.
Thanks.
Use the ControlListener to capture the resize event of the window. If user try to change width of the window restore the default value of it.
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TestWindow {
private static int CONST_WIDTH = 200;
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setBounds(shell.getBounds().x, shell.getBounds().y, CONST_WIDTH, shell.getBounds().height);
shell.setText("Test");
shell.addControlListener(new ControlAdapter() {
@Override
public void controlResized(ControlEvent e) {
Rectangle rect = shell.getBounds();
if(rect.width != CONST_WIDTH) {
shell.setBounds(rect.x, rect.y, CONST_WIDTH, rect.height);
}
}
});
shell.open();
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
精彩评论