Re: Problem with initializing a class object
On Feb 8, 5:51 am, Nigel Wade <n...@ion.le.ac.uk> wrote:
phillip.s.pow...@gmail.com wrote:
> > I am trying to write a more modified version of the infamous
SimpleBrowser by allowing for the WebBrowser object property to
receive a given java.net.URL object.
You've only supplied partial code, so it's only possible to provide guesses as
to what is going wrong.
>
[code]
private final WebBrowser webBrowser = new WebBrowser();
What class does this code belong to, where is it's constructor being invoked?
I will attempt to guess at the problem. I think you probably have a race
condition between the thread which is executing the above constructor for
WebBrowser(), and the code below which is explicitly run on the EDT. It looks
like the code below is attempting to set the URL on the WebBrowser before it
has been properly constructed by whichever thread is constructing it.
You might be right, unfortunately I cannot verify it. However, I can
say that the org.jdesktop.jdic.browser.WebBrowser object works when I
do things this way:
[code]
/**
* {@link org.jdesktop.jdic.browser.WebBrowser}
*/
private WebBrowser browser;
/**
* Perform setup
*/
private void setupSimpleBrowser() {
initObjects();
initComponents();
}
/**
* Initialize objects
*/
private void initObjects() {
WebBrowser.setDebug(false); // SET TO TRUE TO SEE trace()
DEBUG STATEMENTS
browser = new WebBrowser();
}
/**
* Initialize components
*/
private void initComponents() {
setTitle(myName);
webAddressTextField = new JTextField(51);
generateJButton();
p1 = new JPanel(true);
p2 = new JPanel(true); // MUST BE SET BEFORE GOING TO
generateWebBrowser()
generateWebBrowser(); // WILL ADD LOCAL WebBrowser INSTANCE
ONTO JPanel p2 HERE
addToPanel(); // FOR NOW WILL ONLY ADD JPanel p1
forceTFFocus();
addToFrame();
showFrame();
}
/**
* Generate {@link org.jdesktop.jdic.browser.WebBrowser}
*/
private void generateWebBrowser() {
//Use below code to check the status of the navigation
process,
//or register a listener for the notification events.
browser.addWebBrowserListener(
new WebBrowserListener() {
boolean isFirstPage = true;
public void initializationCompleted(WebBrowserEvent event)
{;}
public void downloadStarted(WebBrowserEvent event) {;}
public void downloadCompleted(WebBrowserEvent event) {;}
public void downloadProgress(WebBrowserEvent event) {;}
public void downloadError(WebBrowserEvent event) {;}
public void documentCompleted(WebBrowserEvent event) {
// Uncomment below code to test getContent()/
setContent()/
// executeScript() APIs.
// As the setContent() call will invoke this event,
which falls
// into a loop, so check if this event is fired by the
first
// loaded page.
/*
if (isFirstPage) {
testDOMAPI(browser);
isFirstPage = false;
}
*/
}
public void titleChange(WebBrowserEvent event) {;}
public void statusTextChange(WebBrowserEvent event) {;}
public void windowClose(WebBrowserEvent event) {;}
});
setWebBrowserURL();
}
/**
* Set {@link #browser} with either instantiable {@link
java.net.URL} or with {@link #DEFAULT_URL_PATH}
*/
private void setWebBrowserURL() {
try {
URL url = getURL();
String urlPath = getURLPath();
if (url != null) {
browser.setURL(url);
} else if (urlPath != null && !urlPath.equals("")) {
browser.setURL(new URL(urlPath));
} else {
browser.setURL(new
URL(SimpleBrowser.DEFAULT_URL_PATH));
}
} catch (Exception e) {
try {
browser.setURL(new
URL(SimpleBrowser.DEFAULT_URL_PATH));
} catch (Exception e2) {
e2.printStackTrace();
return;
}
}
}
[/code]
It appears that the crucial element is within initObjects():
WebBrowser.setDebug(false);
webBrowser = new WebBrowser();
In short, you must run the WebBrowser static mehod setDebug() before
you even instantiate a WebBrowser object, otherwise, while the object
will exist regardless, it will not actually set the java.net.URL
parameter unless you have run setDebug() first.
Phil
/**
* Perform setup
*/
private void setupSimpleBrowser() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initObjects();
initComponents();
}
});
}
[/code]
Phil
--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : n...@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555