Mega Code Archive

 
Categories / Java Tutorial / SWT
 

Resize a text control

/*******************************************************************************  * Copyright (c) 2000, 2004 IBM Corporation and others.  * All rights reserved. This program and the accompanying materials  * are made available under the terms of the Eclipse Public License v1.0  * which accompanies this distribution, and is available at  * http://www.eclipse.org/legal/epl-v10.html  *  * Contributors:  *     IBM Corporation - initial API and implementation  *******************************************************************************/   import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.FontMetrics; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class TextControlResize {   public static void main(String[] args) {     Display display = new Display();     Shell shell = new Shell(display);     Text text = new Text(shell, SWT.BORDER);     int columns = 10;     GC gc = new GC(text);     FontMetrics fm = gc.getFontMetrics();     int width = columns * fm.getAverageCharWidth();     int height = fm.getHeight();     gc.dispose();     text.setSize(text.computeSize(width, height));     shell.pack();     shell.open();     while (!shell.isDisposed()) {       if (!display.readAndDispatch())         display.sleep();     }     display.dispose();   } }