Search code examples
javaswingurljeditorpane

Strange behaviour with JEditorPane


I've made a custom class that's extending JEditorPane and makes use of it's setPage()-method. However I've encountered a very strange issue while using it. This is how I've implemented it;

class WebReader extends JEditorPane {

  WebReader(String addressIn) {
    setEditable(false);
    showPage(addressIn)
  }

  void showPage(String address) {    
    try {
      setPage(address);
    } catch (Exception e) {
      e.printStackTrace();
  }
}

A call could look something like this;

WebReader fooReader = new WebReader("https://www.google.com");
fooReader.showPage("https://www.google.comxxxx");

Which isn't supposed to work but mysteriously does.

What's very strange is that it doesn't catch a faulty URL if I've already entered a correct one. For example if I've entered "https://www.google.com", which works fine (as it should) and after that enter https://www.google.comxxxxx, it still displays google.com on my JEditorPane and doesn't raise an exception (which I want it to do).

Worth noting is that if I enter https://www.google.comxxxxx as my 'starting URL', it will indeed raise an exception.

EDIT: Added some more code.


Solution

  • I managed to fix it!

    I added the following to my showPage():

    setEditorKit(createDefaultEditorKit()); 
    

    So it creates a new EditorKit for each time the web-page is changed.