File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Swing + SWT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing + SWT" Watch "Swing + SWT" New topic
Author

Swing + SWT

John Coleman
Ranch Hand

Joined: Jul 24, 2001
Posts: 65
I have a Swing application that I need to pop up an SWT browser, and then be able to change the URL of the browser depending on settings in Swing.

All examples of the SWT browser seem to be a simple single thread, completely pointless examples.

If I try and create more than one SWT display, I get the JVM crashing. Similarly I can't create an SWT browser and then invoke the setURL on it from another thread.

It seems unstable and unfriendly the way it has been implemented.

Is there a way just to create the browser, hide it when it's "closed" and then pop it up again with a new URL (from another thread) as required?

TIA
John


John Coleman, MSTA<br />Sun Certified Programmer for the Java� 2 Platform<br />john.coleman@eurobase-international.com<br />Eurobase banking solutions<br /><a href="http://www.eurobase-international.com/banking" target="_blank" rel="nofollow">http://www.eurobase-international.com/banking</a>
John Coleman
Ranch Hand

Joined: Jul 24, 2001
Posts: 65
This is the code using the asyncExec technique. It crashes the JVM.


import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class SWTBrowser {

private final Display display = new Display();
private final Shell shell = new Shell(display);
private final Browser browser = new Browser(shell, SWT.NONE);

private String aNewsURL;

public SWTBrowser(String newsURL) {
this.aNewsURL = newsURL;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
shell.setLayout(gridLayout);
ToolBar toolbar = new ToolBar(shell, SWT.NONE);
ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
itemBack.setText("Back");
ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
itemForward.setText("Forward");
ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
itemStop.setText("Stop");
ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
itemRefresh.setText("Refresh");
ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
itemGo.setText("Go");

GridData data = new GridData();
data.horizontalSpan = 3;
toolbar.setLayoutData(data);

Label labelAddress = new Label(shell, SWT.NONE);
labelAddress.setText("Address");

final Text location = new Text(shell, SWT.BORDER);
data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.horizontalSpan = 2;
data.grabExcessHorizontalSpace = true;
location.setLayoutData(data);

data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.verticalAlignment = GridData.FILL;
data.horizontalSpan = 3;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
browser.setLayoutData(data);

final Label status = new Label(shell, SWT.NONE);
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
status.setLayoutData(data);

final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
data = new GridData();
data.horizontalAlignment = GridData.END;
progressBar.setLayoutData(data);

/* event handling */
Listener listener = new Listener() {
public void handleEvent(Event event) {
ToolItem item = (ToolItem) event.widget;
String string = item.getText();
if (string.equals("Back")) {
browser.back();
}
else if (string.equals("Forward")) {
browser.forward();
}
else if (string.equals("Stop")) {
browser.stop();
}
else if (string.equals("Refresh")) {
browser.refresh();
}
else if (string.equals("Go")) {
browser.setUrl(location.getText());
}
}
}
;
browser.addProgressListener(new ProgressListener() {
public void changed(ProgressEvent event) {
if (event.total == 0) {
return;
}
int ratio = event.current * 100 / event.total;
progressBar.setSelection(ratio);
}

public void completed(ProgressEvent event) {
progressBar.setSelection(0);
}
});
browser.addStatusTextListener(new StatusTextListener() {
public void changed(StatusTextEvent event) {
status.setText(event.text);
}
});
browser.addLocationListener(new LocationListener() {
public void changed(LocationEvent event) {
if (event.top) {
location.setText(event.location);
}
}

public void changing(LocationEvent event) {
}
});
itemBack.addListener(SWT.Selection, listener);
itemForward.addListener(SWT.Selection, listener);
itemStop.addListener(SWT.Selection, listener);
itemRefresh.addListener(SWT.Selection, listener);
itemGo.addListener(SWT.Selection, listener);
location.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event e) {
browser.setUrl(location.getText());
}
});

display.asyncExec(new Runnable() {
public void run() {
if (!browser.getUrl().equals(aNewsURL)) {
browser.setUrl(aNewsURL);
}
}
});

shell.open();
browser.setUrl(newsURL);

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

public void setURL(String newsURL) {
aNewsURL = newsURL;
}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing + SWT