[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
|
[news.eclipse.platform.swt] Re: Dynamically resizing Text Control
|
Thanks...
Ash
Steve Northover wrote:
> http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/sn
> ippits/snippet111.html
> "Ash" <ash@xxxxxxxxxxxxxxx> wrote in message
> news:b7mvm9$pac$1@xxxxxxxxxxxxxxxx
> > Its great to finally see a newsgroup dedicated to SWT! I am trying to
> > dynamically resize a Text control based on its contents. I basically added
> > a modify listener which calls pack() to recompute the preferred size of
> > the control each time it is modified. The problem I have is after I type
> > the first character, the control resizes but shifts my contents to the
> > left. It seems like the preferred size always needs some extra buffer
> > space at the end.
> >
> > The code snippet I used is shown below. I can get the desired effect by
> > uncommenting the kludge code but I'd like to find a better way if
> possible.
> >
> > import org.eclipse.swt.SWT;
> > import org.eclipse.swt.events.ModifyEvent;
> > import org.eclipse.swt.events.ModifyListener;
> > 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 Name {
> > public static void main(String[] args) {
> > Display display = new Display();
> > final Shell shell = new Shell(display);
> > final Text firstname = new Text(shell, SWT.BORDER);
> > firstname.setText("");
> > firstname.setSize(firstname.computeSize(SWT.DEFAULT, SWT.DEFAULT));
> > firstname.setTextLimit(15);
> > firstname.addModifyListener(new ModifyListener() {
> > public void modifyText(ModifyEvent e) {
> > firstname.pack(true);
> >
> > /* Kludge
> > int columns = firstname.getText().length() + 3;
> > GC gc = new GC (firstname);
> > FontMetrics fm = gc.getFontMetrics ();
> > int width = columns * fm.getAverageCharWidth ();
> > int height = fm.getHeight ();
> > gc.dispose ();
> > firstname.setSize (firstname.computeSize (width, height));
> > */
> > }
> > });
> >
> > shell.pack();
> > shell.open();
> > while (!shell.isDisposed()) {
> > if (!display.readAndDispatch())
> > display.sleep();
> > }
> > display.dispose();
> > }
> > }
> >
> > Ash
> >