WebEngine完成加载 - Javafx

时间:2016-05-02 18:23:44

标签: java javafx javafx-webengine

我正在尝试登录网站,然后加载网址。 这就是我到目前为止所拥有的

package application;
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.*;
import javafx.stage.Stage;
import org.w3c.dom.*;
import org.w3c.dom.html.*;

public class WebViewFormPost extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {

        final BooleanProperty loginAttempted = new SimpleBooleanProperty(false);

        final WebView webView = new WebView();
        webView.setPrefWidth(1000);

       final WebEngine engine = webView.getEngine();
       engine.documentProperty().addListener(new ChangeListener<Document>() {
            @Override
            public void changed(ObservableValue<? extends Document> ov, Document oldDoc, Document doc) {
                if (doc != null && !loginAttempted.get()) {
                    //Submits Form 
                }
            } 
        });
        engine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
            @Override
            public void changed(ObservableValue<? extends Throwable> ov, Throwable oldException, Throwable exception) {
                System.out.println("Load Exception: " + exception);
            }
        });

        Button fxLoginButton = new Button("Login");
        fxLoginButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                if (notEmpty(fxPassword.getText()) && notEmpty(fxPassword.getText())) {
                    loginAttempted.set(false);
                    engine.load("website.com");               
                }
            }
        });
        fxLoginButton.setDefaultButton(true);
        ProgressIndicator fxLoadProgress = new ProgressIndicator(0);
        fxLoadProgress.progressProperty().bind(webView.getEngine().getLoadWorker().progressProperty());
        fxLoadProgress.visibleProperty().bind(webView.getEngine().getLoadWorker().runningProperty());

        HBox loginPane = new HBox(10);
        loginPane.getChildren().setAll(
                fxLoginButton,
                fxLoadProgress
        );


        final VBox layout = new VBox(5);
        layout.setStyle("-fx-background-color: lightgray; -fx-padding: 3;");
        layout.getChildren().addAll(
                loginPane,
                webView
        );

        VBox.setVgrow(webView, Priority.ALWAYS);

        stage.setScene(new Scene(layout));
        stage.show();

        fxUsername.requestFocus();

    }    
}

所以基本上发生的是它加载一个网站(website.com)输入用户名和密码然后提交表格从而登录。登录后我想继续访问网站,直到网站返回成功。换句话说,登录加载网站后,如果没有返回成功,请再次加载网站。但在此过程中,我可能会退出,然后我可以再次登录

0 个答案:

没有答案