Julian H
Julian H

Reputation: 15

Java Swing WebBrowser How to notice that an URL does not excist?

I couldn't find the solution for my question on stackoverflow. Could be that I'm just bad at looking, if so I do apologize.


Anyhow on to the question: I have made my own browser, and I want it to give the user an error if an URL does not excist or not responding. I use the following command from Java Swing: EditorPane = setPage(url) // where EditorPane is a JEditorPane


SetPage(URL url) Only gives me an error if it's not an url sent in the parameter. So how can i get feedback if the URL is not responding?


Thank you in advance, - Julian ps: the comments are in swedish but they are not very helpful!

    private void loadPage(URL url, boolean AddOrNot) {
    try{
        System.out.println("loadPage Try");
        displayEditorPane.setPage(url);           // setter sidan till url

        //URL newUrl = displayEditorPane.getPage(); //ger den nuvarande sidans url funkar ei
        //displayEditorPane.setPage(newUrl); //get error ?!?
        String hist=url.toString();
        textField.setText(hist); // men denna fungerar

        if(AddOrNot){//lägger till url i history-list om du inte använt back / forward knapp
            history.add(hist);
            JMenuItem historyFile = new JMenuItem(hist);
            historyFile.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   textField.setText(hist);
                   position=0;
                   Launch(true);
               } 
            });
            HistoryMenu.add(historyFile);
        }
        //** kollar om forward och back knapparna ska vara aktiva **//
        if(position!=history.size()-1){
            BackButton.setEnabled(true);
        }else{
            BackButton.setEnabled(false);
        }
        if(position>0){
            ForwardButton.setEnabled(true);
        }else{
            ForwardButton.setEnabled(false);
        }


    }catch(IOException io ){
        System.out.println("loadPage Catch " + io);
        msgError("Unable to open page");
    }
}

Upvotes: 1

Views: 201

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168825

displayEditorPane.setPage(url);

If you need more advanced feedback of loading and success, it will be necessary to abandon this simple approach for a more complicated one where the app. takes control of reading the content. For that, look to URLConnection & HttpURLConnection. Methods in particular:

  • URLConnection.getInputStream():

    Returns an input stream that reads from this open connection. A SocketTimeoutException can be thrown when reading from the returned input stream if the read timeout expires before data is available for read.

  • HttpURLConnection.getResponseCode():

    Gets the status code from an HTTP response message. For example, in the case of the following status lines:

    HTTP/1.0 200 OK
    HTTP/1.0 401 Unauthorized
    

    It will return 200 and 401 respectively. Returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP).


For information on dealing with streams, see the Basic I/O lesson of the tutorial.

Upvotes: 1

Related Questions