public void goForward() { try { WebEngine engine = webView.getEngine(); WebHistory history = engine.getHistory(); history.go(1); } catch (Exception e) { } }
// Adding functionality to the forward button public String goForward() { final WebHistory history = browser.getEngine().getHistory(); ObservableList<WebHistory.Entry> entryList = history.getEntries(); int currentIndex = history.getCurrentIndex(); Platform.runLater(() -> history.go(1)); if (currentIndex < entryList.size() - 2) { entryList.get(currentIndex + 1); forwardButton.setDisable(false); } else { entryList.get(history.getCurrentIndex()); forwardButton.setDisable(true); backButton.setDisable(false); } return entryList.get(currentIndex).getUrl(); }
public Browser(final Stage stage) { // apply the styles getStyleClass().add("browser"); for (int i = 0; i < captions.length; i++) { // create hyperlinks Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]); Image image = images[i] = new Image(getClass().getResourceAsStream(imageFiles[i])); hpl.setGraphic(new ImageView(image)); final String url = urls[i]; final boolean addButton = (hpl.getText().equals("Help")); // process event hpl.setOnAction( (ActionEvent e) -> { needDocumentationButton = addButton; webEngine.load(url); }); } comboBox.setPrefWidth(60); // create the toolbar toolBar = new HBox(); toolBar.setAlignment(Pos.CENTER); toolBar.getStyleClass().add("browser-toolbar"); toolBar.getChildren().add(comboBox); toolBar.getChildren().addAll(hpls); toolBar.getChildren().add(createSpacer()); // set action for the button toggleHelpTopics.setOnAction( (ActionEvent t) -> { webEngine.executeScript("toggle_visibility('help_topics')"); }); smallView.setPrefSize(120, 80); // handle popup windows webEngine.setCreatePopupHandler( (PopupFeatures config) -> { smallView.setFontScale(0.8); if (!toolBar.getChildren().contains(smallView)) { toolBar.getChildren().add(smallView); } return smallView.getEngine(); }); // process history final WebHistory history = webEngine.getHistory(); history .getEntries() .addListener( (Change<? extends Entry> c) -> { c.next(); c.getRemoved() .stream() .forEach( (e) -> { comboBox.getItems().remove(e.getUrl()); }); c.getAddedSubList() .stream() .forEach( (e) -> { comboBox.getItems().add(e.getUrl()); }); }); // set the behavior for the history combobox comboBox.setOnAction( (Event ev) -> { int offset = comboBox.getSelectionModel().getSelectedIndex() - history.getCurrentIndex(); history.go(offset); }); // process page loading webEngine .getLoadWorker() .stateProperty() .addListener( (ObservableValue<? extends State> ov, State oldState, State newState) -> { toolBar.getChildren().remove(toggleHelpTopics); if (newState == State.SUCCEEDED) { JSObject win = (JSObject) webEngine.executeScript("window"); win.setMember("app", new JavaApp()); if (needDocumentationButton) { toolBar.getChildren().add(toggleHelpTopics); } } }); // adding context menu final ContextMenu cm = new ContextMenu(); MenuItem cmItem1 = new MenuItem("Print"); cm.getItems().add(cmItem1); toolBar.addEventHandler( MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> { if (e.getButton() == MouseButton.SECONDARY) { cm.show(toolBar, e.getScreenX(), e.getScreenY()); } }); // processing print job cmItem1.setOnAction( (ActionEvent e) -> { PrinterJob job = PrinterJob.createPrinterJob(); if (job != null) { webEngine.print(job); job.endJob(); } }); // load the home page webEngine.load("http://www.cnn.com"); // add components getChildren().add(toolBar); getChildren().add(browser); }
private BorderPane webViewScene() { url = new TextField(); url.setPrefWidth(800); browser = new WebView(); engine = browser.getEngine(); url.setText(fileUrl); engine.load(fileUrl); history = browser.getEngine().getHistory(); backButton = new Button(null, new ImageView(new Image("res/BrowserIcons/back.png"))); backButton.setOnAction( (ActionEvent e) -> { if (!checkIntConnection(defaultUrl)) { alert(); } else browser.getEngine().load(goBack()); }); backButton.addEventHandler( MouseEvent.MOUSE_ENTERED, event -> backButton.setEffect(new DropShadow())); backButton.addEventHandler(MouseEvent.MOUSE_EXITED, event -> backButton.setEffect(null)); forwardButton = new Button(null, new ImageView(new Image("res/BrowserIcons/forward.png"))); forwardButton.setOnAction( (ActionEvent e) -> { if (!checkIntConnection(defaultUrl)) { alert(); } else browser.getEngine().load(goForward()); }); forwardButton.addEventHandler( MouseEvent.MOUSE_ENTERED, event -> forwardButton.setEffect(new DropShadow())); forwardButton.addEventHandler(MouseEvent.MOUSE_EXITED, event -> forwardButton.setEffect(null)); reloadButton = new Button(null, new ImageView(new Image("res/BrowserIcons/reload.png"))); reloadButton.setOnAction( event -> { if (!checkIntConnection(defaultUrl)) { alert(); } else browser.getEngine().reload(); }); reloadButton.addEventHandler( MouseEvent.MOUSE_ENTERED, event -> reloadButton.setEffect(new DropShadow())); reloadButton.addEventHandler(MouseEvent.MOUSE_EXITED, event -> reloadButton.setEffect(null)); goButton = new Button(null, new ImageView(new Image("res/BrowserIcons/go.jpg"))); goButton.setOnAction( event -> { // adding the http or https prefix if user didn't type it if (!checkIntConnection(defaultUrl)) { alert(); } else { if (url.getText().length() > 7 && (url.getText(0, 7).equals("http://") || url.getText(0, 8).equals("https://"))) { browser.getEngine().load(url.getText()); } else if (url.getText().length() > 7 && !url.getText(0, 7).equals("http://")) { browser.getEngine().load("http://" + url.getText()); } else { browser.getEngine().load("https://" + url.getText()); } } }); goButton.addEventHandler( MouseEvent.MOUSE_ENTERED, event -> goButton.setEffect(new DropShadow())); goButton.addEventHandler(MouseEvent.MOUSE_EXITED, event -> goButton.setEffect(null)); webHistoryComboBox = new ComboBox(); webHistoryComboBox.setPromptText("Web History"); webHistoryComboBox.setPrefWidth(200); webHistoryComboBox.setOnAction( event -> { // Navigate to the link in the history index int offset = webHistoryComboBox.getSelectionModel().getSelectedIndex() - history.getCurrentIndex(); history.go(offset); }); // Displying browse history in a combo box history .getEntries() .addListener( (ListChangeListener<WebHistory.Entry>) c -> { c.next(); for (WebHistory.Entry e : c.getRemoved()) webHistoryComboBox.getItems().remove(e.getUrl()); for (WebHistory.Entry e : c.getAddedSubList()) webHistoryComboBox.getItems().add(e.getUrl()); }); // Enabling and Disabling back and forward buttons browser .getEngine() .getLoadWorker() .stateProperty() .addListener( (observable, oldValue, newValue) -> { if (!browser.getEngine().getHistory().getEntries().isEmpty()) { int index = browser.getEngine().getHistory().getCurrentIndex(); url.setText(browser.getEngine().getHistory().getEntries().get(index).getUrl()); if (index == 0) { backButton.setDisable(true); } else { backButton.setDisable(false); } if (browser.getEngine().getHistory().getEntries().size() == 1) { forwardButton.setDisable(true); } else if (index < browser.getEngine().getHistory().getEntries().size() - 2) { forwardButton.setDisable(false); } else if (index < browser.getEngine().getHistory().getEntries().size() - 1) { forwardButton.setDisable(false); } else if (index == browser.getEngine().getHistory().getEntries().size() - 1) { forwardButton.setDisable(true); } } }); progress.setPrefSize(25, 25); progress.visibleProperty().bind(browser.getEngine().getLoadWorker().runningProperty()); BorderPane border = new BorderPane(); HBox topBar = new HBox(); topBar.setAlignment(Pos.CENTER); topBar.setSpacing(10); topBar.setPadding(new Insets(10, 10, 10, 10)); topBar .getChildren() .addAll( progress, reloadButton, url, goButton, backButton, forwardButton, webHistoryComboBox); border.setTop(topBar); border.setCenter(browser); border.setPrefHeight(Screen.getPrimary().getVisualBounds().getHeight()); border.setPrefWidth(Screen.getPrimary().getVisualBounds().getWidth()); return border; }