|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package carmelo.javafx; |
| 7 | + |
| 8 | +import javafx.application.Application; |
| 9 | +import javafx.geometry.Insets; |
| 10 | +import javafx.geometry.Pos; |
| 11 | +import javafx.scene.Node; |
| 12 | +import javafx.scene.Scene; |
| 13 | +import javafx.scene.control.Button; |
| 14 | +import javafx.scene.control.Label; |
| 15 | +import javafx.scene.control.ProgressBar; |
| 16 | +import javafx.scene.control.TextField; |
| 17 | +import javafx.scene.layout.HBox; |
| 18 | +import javafx.scene.layout.Priority; |
| 19 | +import javafx.scene.layout.VBox; |
| 20 | +import javafx.scene.web.WebEngine; |
| 21 | +import javafx.scene.web.WebHistory; |
| 22 | +import javafx.scene.web.WebView; |
| 23 | +import javafx.stage.Stage; |
| 24 | + |
| 25 | +/** |
| 26 | + * |
| 27 | + * @author Carmelo Marín Abrego |
| 28 | + */ |
| 29 | +public class JavaFXWebView extends Application { |
| 30 | + |
| 31 | + public Node createTopBar(WebEngine webEngine) { |
| 32 | + HBox root = new HBox(); |
| 33 | + root.setSpacing(5.0); |
| 34 | + root.setAlignment(Pos.CENTER); |
| 35 | + |
| 36 | + Label urlLabel = new Label("URL"); |
| 37 | + |
| 38 | + TextField urlText = new TextField("http://www.google.com"); |
| 39 | + HBox.setHgrow(urlText, Priority.ALWAYS); |
| 40 | + |
| 41 | + Button btnIr = new Button("Ir"); |
| 42 | + Button btnNext = new Button("Siguiente"); |
| 43 | + Button btnPrev = new Button("Anterior"); |
| 44 | + |
| 45 | + btnPrev.setDisable(true); |
| 46 | + btnNext.setDisable(true); |
| 47 | + |
| 48 | + WebHistory history = webEngine.getHistory(); |
| 49 | + |
| 50 | + history.currentIndexProperty().addListener((p, oldValue, newValue) -> { |
| 51 | + int currentIndex = newValue.intValue(); |
| 52 | + |
| 53 | + if (currentIndex <= 0) { |
| 54 | + btnPrev.setDisable(true); |
| 55 | + } else { |
| 56 | + btnPrev.setDisable(false); |
| 57 | + } |
| 58 | + |
| 59 | + if (currentIndex >= history.getEntries().size() - 1) { |
| 60 | + btnNext.setDisable(true); |
| 61 | + } else { |
| 62 | + btnNext.setDisable(false); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + btnIr.setOnAction(a -> webEngine.load(urlText.getText())); |
| 67 | + |
| 68 | + btnNext.setOnAction(a -> history.go(+1)); |
| 69 | + btnPrev.setOnAction(a -> history.go(-1)); |
| 70 | + |
| 71 | + root.getChildren().addAll( |
| 72 | + urlLabel, urlText, |
| 73 | + btnIr, btnPrev, btnNext |
| 74 | + ); |
| 75 | + |
| 76 | + return root; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void start(Stage primaryStage) { |
| 81 | + |
| 82 | + ProgressBar progressBar = new ProgressBar(0); |
| 83 | + progressBar.setMaxWidth(Double.MAX_VALUE); |
| 84 | + |
| 85 | + WebView webView = new WebView(); |
| 86 | + |
| 87 | + WebEngine webEngine = webView.getEngine(); |
| 88 | + |
| 89 | + Node topBar = createTopBar(webEngine); |
| 90 | + |
| 91 | + // cambiar el titulo al cargar una nueva pagina web |
| 92 | + webEngine.titleProperty() |
| 93 | + .addListener((p, o, t) -> primaryStage.setTitle(t)); |
| 94 | + |
| 95 | + // mostrar el progreso de cargar de la web |
| 96 | + webEngine.getLoadWorker().progressProperty() |
| 97 | + .addListener((p, o, v) -> progressBar.setProgress(v.doubleValue())); |
| 98 | + |
| 99 | + VBox root = new VBox(); |
| 100 | + root.getChildren().addAll(topBar, webView, progressBar); |
| 101 | + root.setPadding(new Insets(5.0)); |
| 102 | + root.setSpacing(10.0); |
| 103 | + |
| 104 | + Scene scene = new Scene(root, 800, 420); |
| 105 | + |
| 106 | + primaryStage.setTitle("JavaFX Navegador Web"); |
| 107 | + primaryStage.setScene(scene); |
| 108 | + primaryStage.show(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param args the command line arguments |
| 113 | + */ |
| 114 | + public static void main(String[] args) { |
| 115 | + launch(args); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments