[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
|
[news.eclipse.platform.swt] Re: Dynamically resizing Text Control
|
This is native behavior on win32 that is really annoying. I have the same
problem in the GEF Logic example. Despite resizing the Text control to be
large enough to accomodate the string, it still leaves the text shifted to
the left. There are 2 workarounds:
1) make the control as wide as it wants to be PLUS the width of the largest
possible character one might type (of course, pasting into the control will
still cause the problem). Use computeSize(-1,-1) instead of pack to figure
out how big it wants to be.
2) After sizing the control, bounce the caret back to the beginning of the
control to cause the text to shift back into view, then set the caret back
to where it was. This will BREAK text input in certain locales which
require DME. setRedraw(true/false) can be used with this workaround to
avoid the resulting flicker.
3) Show that this problem doesn't exist on another SWT platform, then open a
bugzilla asking that the SWT team provide a workaround in the win32
Text.setBounds(....)
"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
>