Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » getPreferredSize() not returning proper size until after invalidating layout
getPreferredSize() not returning proper size until after invalidating layout [message #196879] Tue, 27 September 2005 18:28 Go to next message
Brad Reynolds is currently offline Brad ReynoldsFriend
Messages: 309
Registered: July 2009
Senior Member
When I run the following snippet the preferred size for the label will
be the same even after multiple sets of the text until I invalidate the
layout. It renders fine but when calling getPreferredSize() I'm not
being returned the proper size. I have the need to set the text on a
Label (perhaps a few times) and then afterward to retrieve an accurate
preferred size. Is this the correct way to be doing this? Is there a
better way? From what I can tell the issue is because in invalidate()
of Figure it checks to see if the figure is valid, if not it won't
invalidate the layout. After setting the text the Label becomes invalid
and thus won't invalidate the figure again and won't update the layout
of my parent figure and therefore won't return the correct size.

public class ValidTest {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());

FigureCanvas canvas = new FigureCanvas(shell);
Figure content = new Figure();
content.setLayoutManager(new FlowLayout());
canvas.setContents(content);

Figure figure = new Figure();
content.add(figure);

StackLayout layout = new StackLayout();
figure.setLayoutManager(layout);

Label label = new Label("text"); //$NON-NLS-1$
figure.add(label);

System.out.println(figure.getPreferredSize());

label.setText("text text"); //$NON-NLS-1$

//Uncomment to retrieve the correct preferred size.
// layout.invalidate();

System.out.println(figure.getPreferredSize());

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

display.dispose();
}
}


Thanks,
Brad
Re: getPreferredSize() not returning proper size until after invalidating layout [message #197083 is a reply to message #196879] Thu, 29 September 2005 13:39 Go to previous messageGo to next message
Brad Reynolds is currently offline Brad ReynoldsFriend
Messages: 309
Registered: July 2009
Senior Member
Any ideas? I'm just wanting to make sure that I'm not doing something I
shouldn't be. If there's a better way to do this I'd appreciate the
input. Thanks.

Brad Reynolds wrote:
> When I run the following snippet the preferred size for the label will
> be the same even after multiple sets of the text until I invalidate the
> layout. It renders fine but when calling getPreferredSize() I'm not
> being returned the proper size. I have the need to set the text on a
> Label (perhaps a few times) and then afterward to retrieve an accurate
> preferred size. Is this the correct way to be doing this? Is there a
> better way? From what I can tell the issue is because in invalidate()
> of Figure it checks to see if the figure is valid, if not it won't
> invalidate the layout. After setting the text the Label becomes invalid
> and thus won't invalidate the figure again and won't update the layout
> of my parent figure and therefore won't return the correct size.
>
> public class ValidTest {
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
>
> FigureCanvas canvas = new FigureCanvas(shell);
> Figure content = new Figure();
> content.setLayoutManager(new FlowLayout());
> canvas.setContents(content);
>
> Figure figure = new Figure();
> content.add(figure);
>
> StackLayout layout = new StackLayout();
> figure.setLayoutManager(layout);
>
> Label label = new Label("text"); //$NON-NLS-1$
> figure.add(label);
>
> System.out.println(figure.getPreferredSize());
>
> label.setText("text text"); //$NON-NLS-1$
>
> //Uncomment to retrieve the correct preferred size.
> // layout.invalidate();
>
> System.out.println(figure.getPreferredSize());
>
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
>
> display.dispose();
> }
> }
>
>
> Thanks,
> Brad
Re: getPreferredSize() not returning proper size until after invalidating [message #197281 is a reply to message #197083] Fri, 30 September 2005 16:15 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: alex_blewitt.yahoo.com

It might be a bug. If you raise it on http://bugs.eclipse.org then someone can look into it there. If it's not a bug then the SWT guys processing it will know what you're doing wrong, especially if you've got a snippet of code to test against.
Re: getPreferredSize() not returning proper size until after invalidating layout [message #197459 is a reply to message #197083] Mon, 03 October 2005 17:42 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: none.unknown.com

The problem is that the figure hierarchy hasn't had a chance to validate
yet. When it's in the invalid state, there's no guarantee that the results
are correct. So, you can either force the validation to happen right away,
as follows:

canvas.getLightweightSystem().getRootFigure().validate();
OR
canvas.getLightweightSystem().getUpdateManager().performUpda te();

Or you can just wait for the update manager to validate the figures
asynchronously on the Display thread and just get your preferred size
afterwards. As follows:

display.asyncExec(new Runnable() {
public void run() {
System.out.println(figure.getPreferredSize());
}
});

Since you're queuing your code asynchronously on the display thread as well
in the above example, you're guaranteed that the UpdateManager would've had
a chance to run before your code is executed.

"Brad Reynolds" <bradleyjames@gmail.com> wrote in message
news:433BEEA5.7060504@gmail.com...
> Any ideas? I'm just wanting to make sure that I'm not doing something I
> shouldn't be. If there's a better way to do this I'd appreciate the
> input. Thanks.
>
> Brad Reynolds wrote:
> > When I run the following snippet the preferred size for the label will
> > be the same even after multiple sets of the text until I invalidate the
> > layout. It renders fine but when calling getPreferredSize() I'm not
> > being returned the proper size. I have the need to set the text on a
> > Label (perhaps a few times) and then afterward to retrieve an accurate
> > preferred size. Is this the correct way to be doing this? Is there a
> > better way? From what I can tell the issue is because in invalidate()
> > of Figure it checks to see if the figure is valid, if not it won't
> > invalidate the layout. After setting the text the Label becomes invalid
> > and thus won't invalidate the figure again and won't update the layout
> > of my parent figure and therefore won't return the correct size.
> >
> > public class ValidTest {
> > public static void main(String[] args) {
> > Display display = new Display();
> > Shell shell = new Shell(display);
> > shell.setLayout(new FillLayout());
> >
> > FigureCanvas canvas = new FigureCanvas(shell);
> > Figure content = new Figure();
> > content.setLayoutManager(new FlowLayout());
> > canvas.setContents(content);
> >
> > Figure figure = new Figure();
> > content.add(figure);
> >
> > StackLayout layout = new StackLayout();
> > figure.setLayoutManager(layout);
> >
> > Label label = new Label("text"); //$NON-NLS-1$
> > figure.add(label);
> >
> > System.out.println(figure.getPreferredSize());
> >
> > label.setText("text text"); //$NON-NLS-1$
> >
> > //Uncomment to retrieve the correct preferred size.
> > // layout.invalidate();
> >
> > System.out.println(figure.getPreferredSize());
> >
> > shell.open();
> > while (!shell.isDisposed()) {
> > if (!display.readAndDispatch())
> > display.sleep();
> > }
> >
> > display.dispose();
> > }
> > }
> >
> >
> > Thanks,
> > Brad
Re: getPreferredSize() not returning proper size until after invalidating layout [message #197594 is a reply to message #196879] Tue, 04 October 2005 13:56 Go to previous message
Eclipse UserFriend
Originally posted by: none.us.ibm.com

This sounds like a bug. Please file a bugzilla.

"Brad Reynolds" <bradleyjames@gmail.com> wrote in message
news:dhc2vf$fgh$1@news.eclipse.org...
> When I run the following snippet the preferred size for the label will be
> the same even after multiple sets of the text until I invalidate the
> layout. It renders fine but when calling getPreferredSize() I'm not being
> returned the proper size. I have the need to set the text on a Label
> (perhaps a few times) and then afterward to retrieve an accurate preferred
> size. Is this the correct way to be doing this? Is there a better way?
> From what I can tell the issue is because in invalidate() of Figure it
> checks to see if the figure is valid, if not it won't invalidate the
> layout. After setting the text the Label becomes invalid and thus won't
> invalidate the figure again and won't update the layout of my parent
> figure and therefore won't return the correct size.
>
> public class ValidTest {
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
>
> FigureCanvas canvas = new FigureCanvas(shell);
> Figure content = new Figure();
> content.setLayoutManager(new FlowLayout());
> canvas.setContents(content);
>
> Figure figure = new Figure();
> content.add(figure);
>
> StackLayout layout = new StackLayout();
> figure.setLayoutManager(layout);
> Label label = new Label("text"); //$NON-NLS-1$
> figure.add(label);
>
> System.out.println(figure.getPreferredSize());
>
> label.setText("text text"); //$NON-NLS-1$
>
> //Uncomment to retrieve the correct preferred size.
> // layout.invalidate();
>
> System.out.println(figure.getPreferredSize());
>
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
>
> display.dispose();
> }
> }
>
>
> Thanks,
> Brad
Previous Topic:closing editors
Next Topic:Linux: java.lang.ClassNotFoundException: java.text.Bidi when opening a gef editor.
Goto Forum:
  


Current Time: Thu Jan 16 16:40:35 GMT 2025

Powered by FUDForum. Page generated in 0.04259 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top