Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an SWT Composite sometimes require a call to resize() to layout correctly?

Tags:

java

swt

Sometimes we encounter an SWT composite that absolutely refuses to lay itself out correctly. Often we encounter this when we have called dispose on a composite, and then replaced it with another; although it does not seem to be strictly limited to this case.

When we run into this problem, about 50 % of the time, we can call pack() and layout() on the offending composite, and all will be well. About 50 % of the time, though, we have to do this:

Point p = c.getSize();
c.setSize(p.x+1, p.y+1);
c.setSize(p);

We've had this happen with just about every combination of layout managers and such.

I wish I had a nice, simple, reproducible case, but I don't. I'm hoping that someone will recognize this problem and say: "Well, duh, you're missing xyz...."

like image 288
Jared Avatar asked Feb 25 '09 15:02

Jared


People also ask

What is SWT composite?

Composite is an ancestor of every SWT class to which you can add widgets, including Shell . For that reason, you can think of a Composite as being simply the working area of a Shell , without the styles associated with the aspects of Shell that make windows—the titlebar, control menu, etc.

What is an SWT application?

The Standard Widget Toolkit (SWT) is a Java based user interface library for developing desktop application. SWT supports Windows, Linux and Mac OS X. It provides lots of standard widgets, e.g., buttons and text fields as well as the option to create custom widgets.

What is Shell in SWT?

public Shell(Display display) Constructs a new instance of this class given only the display to create it on. It is created with style SWT.


1 Answers

Looks to me like the layout's cache is outdated and needs to be refreshed.

Layouts in SWT support caches, and will usually cache preferred sizes of the Controls, or whatever they like to cache:

public abstract class Layout {     protected abstract Point computeSize (Composite composite, int wHint, int hHint, boolean flushCache);     protected boolean flushCache (Control control) {...}     protected abstract void layout (Composite composite, boolean flushCache); } 

I'm relatively new to SWT programming (former Swing programmer), but encountered similar situations in which the layout wasn't properly updated. I was usually able to resolve them using the other layout methods that will also cause the layout to flush its cache:

layout(boolean changed)  layout(boolean changed, boolean allChildren) 
like image 157
Peter Walser Avatar answered Oct 07 '22 14:10

Peter Walser