Wait for Browser thread to return [message #757204] |
Thu, 17 November 2011 05:55 |
andzsinszan Messages: 32 Registered: August 2011 |
Member |
|
|
Hi,
Is there a way to wait for the Browser rendering thread to return after dispatching with a setText(String s)? I am changing the Browser's contents programmatically with Browser#setText, so there isn't any delay that would require a separate thread.
I would prefer to wait for the Browser thread to return after rendering the html, in order to update the widget's preferred size.
While I can update the the size from Browser#addProgressListener it results in a flickering:
paint#1 "normal" paint
paint#2 paint after browser is finished, size are calculated
In this particular example there is flickering - because there is no resize.
The resize event would take place where STDOUT says "Browser ready".
A working example:
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class SwtBrowserThread extends Composite{
final Browser browser ;
int i = 0;
public SwtBrowserThread (Composite parent){
super(parent, SWT.NONE);
setLayout(new FillLayout(SWT.VERTICAL));
browser = new Browser(this, SWT.NONE);
browser.addProgressListener(new ProgressListener() {
@Override
public void completed(ProgressEvent arg0){
System.out.println("Browser ready!");
}
@Override
public void changed(ProgressEvent arg0) {
}
});
Button btn = new Button(this, SWT.PUSH);
btn.setText("Change");
btn.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event arg0) {
System.out.println("\nBefore setText()");
browser.setText("Changed " + i++ );
System.out.println("After setText()");
}
});
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Composite panel = new SwtBrowserThread(shell);
shell.setSize(200,120);
shell.open();
while(!shell.isDisposed()){
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
|
|
|
|
Re: Wait for Browser thread to return [message #757932 is a reply to message #757204] |
Thu, 17 November 2011 15:27 |
Grant Gayed Messages: 2150 Registered: July 2009 |
Senior Member |
|
|
Hi,
Listening to ProgressListener.completed() is the correct place to
receive notification of this.
Something you can try is to add "browser.setRedraw(false);" when
invoking setText(), and then when ProgressListener.completed() is
received you can resize it if needed and then invoke
"browser.setRedraw(true);" to display its content.
HTH,
Grant
On 11/17/2011 12:55 AM, andzsinszan wrote:
> Hi,
>
> Is there a way to wait for the Browser rendering thread to return after
> dispatching with a setText(String s)? I am changing the Browser's
> contents programmatically with Browser#setText, so there isn't any delay
> that would require a separate thread.
> I would prefer to wait for the Browser thread to return after rendering
> the html, in order to update the widget's preferred size.
>
> While I can update the the size from Browser#addProgressListener it
> results in a flickering: paint#1 "normal" paint
> paint#2 paint after browser is finished, size are calculated
>
> In this particular example there is flickering - because there is no
> resize.
> The resize event would take place where STDOUT says "Browser ready".
>
>
> A working example:
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.browser.Browser;
> import org.eclipse.swt.browser.ProgressEvent;
> import org.eclipse.swt.browser.ProgressListener;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.swt.widgets.Listener;
> import org.eclipse.swt.widgets.Shell;
>
> public class SwtBrowserThread extends Composite{
>
> final Browser browser ;
> int i = 0;
> public SwtBrowserThread (Composite parent){
> super(parent, SWT.NONE);
> setLayout(new FillLayout(SWT.VERTICAL));
>
> browser = new Browser(this, SWT.NONE);
> browser.addProgressListener(new ProgressListener() {
> @Override
> public void completed(ProgressEvent arg0){
> System.out.println("Browser ready!");
> }
> @Override
> public void changed(ProgressEvent arg0) {
> }
> });
>
> Button btn = new Button(this, SWT.PUSH);
> btn.setText("Change");
> btn.addListener(SWT.Selection, new Listener() {
> @Override
> public void handleEvent(Event arg0) {
> System.out.println("\nBefore setText()");
> browser.setText("Changed " + i++ );
> System.out.println("After setText()");
> }
> });
> }
>
>
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
>
>
> Composite panel = new SwtBrowserThread(shell);
> shell.setSize(200,120);
> shell.open();
> while(!shell.isDisposed()){
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
>
> }
> }
>
>
>
>
|
|
|
|
Powered by
FUDForum. Page generated in 0.08112 seconds