pravi
pravi

Reputation: 444

Refreshing JEditorPane with new data

I am trying to refresh JEditorPane every 5 seconds if new data added to database table it should get reflected, But its not repainting.

Firstly i have a JFrame which is as below which is called inside a constructor

public Browser(String initialURL) {  

    this.initialURL = initialURL;
    WindowUtilities.setNativeLookAndFeel();
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    topPanel = new JPanel();
    urlField = new JTextField(30);
    getContentPane().add(topPanel, BorderLayout.NORTH); 

    try {
        htmlPane = new JEditorPane(initialURL);
        htmlPane.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(htmlPane);
        scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
    } catch(IOException ioe) {
       warnUser("Can't build HTML pane for " + initialURL 
                + ": " + ioe);
    }

    Dimension screenSize = getToolkit().getScreenSize();
    setBounds(400, 300, 600, 400);
    setVisible(true);
}

For launching this i have created one singlton class as below and passing url of xhtml page to be loaded which picks the data from database.

public class BrowserInstance {

    public static Browser browser ;

    public static synchronized Browser getInstance(String serverpath)
    {
        if(browser == null)
        {
            browser = new Browser(serverpath);
        }
        else if(browser != null)
        {
            try {
                browser.getHtmlPane().setEditable(false);
                browser.setVisible(true);
                browser.getHtmlPane().setPage(serverpath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return browser;
    }

}

But for some reason its not refreshing page inside popup . I'm able to call this singlton class every 5 second using ScheduledExecutorService concept your help will be greatly appreciated.

Upvotes: 1

Views: 1288

Answers (1)

blackbishop
blackbishop

Reputation: 32700

If you're passing the same url as the one which is currently displayed by the EditorPane, the document will not be reloaded. So you have to force the document reload by clearing the stream description property of the document (see setPage() method).

You have just to add the following code :

Document document = browser.getHtmlPane().getDocument();
document.putProperty(Document.StreamDescriptionProperty, null);

Upvotes: 3

Related Questions