Re: How do you time resetting a Component message at a precise time?
On Mar 9, 12:48 pm, "phillip.s.pow...@gmail.com"
<phillip.s.pow...@gmail.com> wrote:
After my JEditorPane has finished loading the URL, I want to reset a
message found within a StatusBar class (extends JLabel) below the
JEditorPane. Problem is, each time I try, the message is instantly
reset even before the entire JFrame is displayed.
This is the exact order I want to have happen:
1) Preloads StatusBar message with "Attempting to loadhttp://www.blah.com"
2) Attempts to actually load JEditorPane with "http://www.blah.com"
[code]
/**
* Generate web browser
*/
private void generateWebBrowser() {
SimpleBrowser.this.browser.setEditable(false);
SimpleBrowser.this.browser.setContentType("text/html");
Dimension dim = new
Dimension(SimpleBrowser.this.getScreenWidth(),
SimpleBrowser.this.manager.calculatePreferredHeight());
SimpleBrowser.this.browser.setSize(dim);
SimpleBrowser.this.browser.setMaximumSize(dim);
SimpleBrowser.this.browser.setPreferredSize(dim);
if (!this.hasGeneratedHyperlinkListener) {
// THIS IS TO ENSURE ONLY ONE HyperlinkListener
INSTANCE EXISTS WITHIN SimpleBrowser.this.browser JEditorPane
this.hasGeneratedHyperlinkListener = true;
SimpleBrowser.this.browser.addHyperlinkListener(new
HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent evt) {
if (evt.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED) {
SimpleBrowser.this.setURL(evt.getURL());
SwingUtilities.invokeLater(new Runnable()
{
public void run() {
try {
SimpleBrowser.this.statusBar.setMessage("Attempting to load " +
SimpleBrowser.this.getURL().toString());
SimpleBrowser.this.processor.processLink(SimpleBrowser.this.getURL());
} catch (Exception e) {
e.printStackTrace();
SimpleBrowser.this.handleErrors();
}
}
});
}
}
});
}
SimpleBrowser.this.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 (urlPath != null && !urlPath.equals("")) {
setURL(new URL(urlPath));
} else {
setURL(new URL(SimpleBrowser.DEFAULT_URL_PATH));
}
SimpleBrowser.this.statusBar.setMessage("Attempting to
load " + SimpleBrowser.this.getURLPath());
} catch (Exception e) {
e.printStackTrace();
handleErrors();
} finally {
try {
if (urlPath != null && !urlPath.equals("")) {
setURL(new URL(urlPath));
} else {
setURL(new URL(SimpleBrowser.DEFAULT_URL_PATH));
}
browser.setPage(getURL());
} catch (ConnectException e) {
handleErrors(getURL(), e);
System.exit(0);
} catch (UnknownHostException e2) {
handleErrors(getURL(), e2);
System.exit(0);
} catch (Exception e3) {
e3.printStackTrace();
handleErrors();
System.exit(0);
}
}
}
[/code]
3) Display entire JFrame (I assume via setVisible(true)) with
"Attempting to load.." as it continues to load the URL within the
Thread
4) Once the load is completed, then change the StatusBar message to
"Done".
[code]
/**
* Show {@link SimpleBrowser}
*/
public void showFrame() {
// CODE BORROWED FROMhttp://today.java.net/pub/a/today/2003/12/08/swing.html
WITH MODIFICATIONS
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException e) {
System.out.println("Unable to load native look and
feel");
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
} catch (InstantiationException e3) {
e3.printStackTrace();
} catch (IllegalAccessException e4) {
e4.printStackTrace();
}
SimpleBrowser.this.pack();
SimpleBrowser.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SimpleBrowser.this.setVisible(true);
SimpleBrowser.this.statusBar.setMessage("Done");
}
[/code]
I included code that, to the best of my "knowledge", would come
closest to implementing the desired order, though in testing it
doesn't; all you see is "Done" and no other type of message.
Is there a way to accomplish this?
Thanx
Phil
**UPDATE**
I tried using a Task class I wrote which extends SwingWorker<Void,
Void>:
Task task = new Task() {
public Void doInBackground() {
SimpleBrowser.this.statusBar.setMessage("Attempting to load " +
SimpleBrowser.this.getURL().toString());
int progress = 0;
//Initialize progress property.
setProgress(0);
while (progress < 10 && !
SimpleBrowser.this.builder.hasLoadedWebpage) {
// SLEEP FOR 1 SECOND
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {}
progress++;
setProgress(Math.min(progress, 10));
}
SimpleBrowser.this.setWebBrowserURL();
return null;
}
public void done() {
SimpleBrowser.this.statusBar.setMessage("Done");
}
};
task.addPropertyChangeListener(SimpleBrowser.this);
task.execute();
Problem is now that it will do the following:
1) Show status of "Attempting to load.."
2) Show status of "Done"
3) Load the page into JEditorPane
The order I want is this:
1) Show status of "Attempting to load.."
2) Load the page into JEditorPane
3) Show status of "Done"
How do I accomplish this?
Phil