JavaFX WebEngine: how can I react to a wrong url?

时间:2015-07-31 19:46:43

标签: url javafx javafx-webengine

I made a little Browser for me with Netbeans and XML. I use a TextField to put in the url manually. If I fill in a wrong url (Example http://www.usususususuuuuuuu.com) the broser is busy but there is no reaction. How can handle this status. It is the handleSearchButton(ActionEvent event)-method that interesting for me.

Thanks a lot!

public class FXMLDocumentController implements Initializable {

private Label label;
@FXML
private Button buttonHome;
@FXML
private Button buttonSearch;
private ToggleButton favorites;
@FXML
private Label labelStatus;
@FXML
private WebView webView;
private WebEngine browser;
@FXML
private TextField textField;
@FXML
private ProgressBar progressBar;
@FXML
private ChoiceBox<String> favBox;
private ObservableList<String> olist;
private TreeSet<String> list;
@FXML
private Button RemoveFavorit;

@Override
public void initialize(URL url, ResourceBundle rb) {

    browser = this.webView.getEngine();

    browser.load("http://www.donau-uni.ac.at");
    this.handleLoadingState();

    list = new TreeSet<>();

    olist = FXCollections.observableArrayList(list);


    favBox.getItems().clear();
    favBox.getItems().addAll(list);

}

@FXML
private void handleHomeButton(ActionEvent event) {
    this.handleLoadingState();

    browser.load("http://www.donau-uni.ac.at");
}

@FXML
private void handleSearchButton(ActionEvent event) {

    this.handleLoadingState();
    String url = this.textField.getText();
    if (!url.startsWith("http://")) {
        url = "http://" + url;

    }
     browser.load(url);



}

1 个答案:

答案 0 :(得分:1)

You can register a listener with

browser.getLoadWorker().stateProperty()

The state will change to Worker.State.FAILED if the URL is incorrect (or if loading fails for any reason).