开发者

How can I tell if a JEditorPane/TextPane document or page has finished loading?

I'm trying to load a URL into a JEditorPane or JTextPane but the URL is a dynamically generated PHP page. I then want to process the output from the PHP page in my Java application. The PHP page will always output at least one String that I can use to check that it's generated correctly.

If I try and process the page straight after setting the page using

 JEditorPane.setPage(URL);
 if( outputTracker.getText().contains("desktop_process") )

it returns a blank HTML page, even if I specify a text/plain content type for the JEditorPane:

System.out.println(outputTracker.getText() );

I assume this is because the PHP page hasn't finished loading as the method below will return the correct output every time.

At the moment I'm setting the page and then starting a Swing Timer and checking every 200ms if the page contains the correctly generated String:

private void getPageBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    outputTracker.setPage("URL_GOES_HERE?variables=x&y=a");
    check_response_timer.start();
}

ActionListener checkAction = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        if( outputTracker.getText().contains("desktop_process") ) {
            Syste开发者_C百科m.out.println("Checking results...");
            check_response_timer.stop();
            process_response();
        } else {
            num_checks++;
            System.out.println("Checking results...");
            if( num_checks == 10 ) {
                check_response_timer.stop();
                checkLater = true;
                responseLbl.setText("Connection timeout. Please click reconnect to check again.");
                num_checks = 0;
            }
        }
    }
};
private Timer check_response_timer = new Timer(200,checkAction);

This works fine. At current server loads and the current complexity of the PHP pages being loaded the loop stops after the first iteration but it just seems a little inefficient and I don't want to have to keep checking if the page is going to take several seconds to load during heavy server loads or on more complex PHP pages.

I'm wondering if there's a better way to check that the page has finished loading.

I was wondering if I could use the JEditorPane.read(inputStream) method or set the page in a Swing Background Worker thread and then process the output of the PHP page when the worker's done() is called.

And is there a better way of loading the PHP output, reading it straight into a string from the dynamically generated output without the use of an editor pane as the editorpane isn't visible anyway?

Hope this is clear enough.

Thanks.


You can add a PropertyChangeListener to the editor pane:

editorPane.addPropertyChangeListener("page", ...);

However, this will only tell you when the main page is loaded. It won't tell you when images or other related files have finised loading.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜