Last active
April 15, 2024 21:47
-
-
Save trashgod/0fc8eb920ba9ee5e04b2810a9176d440 to your computer and use it in GitHub Desktop.
WebViewPane—display a WebView that fills the enclosing pane.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.application.Platform; | |
import javafx.collections.ObservableList; | |
import javafx.event.ActionEvent; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.TextField; | |
import javafx.scene.control.ToolBar; | |
import javafx.scene.input.KeyCode; | |
import javafx.scene.input.KeyCombination; | |
import javafx.scene.input.KeyEvent; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.web.WebEngine; | |
import javafx.scene.web.WebHistory; | |
import javafx.scene.web.WebView; | |
import javafx.stage.Stage; | |
/** | |
* @see https://stackoverflow.com/a/12714730/230513 | |
* @see https://stackoverflow.com/a/44133281/230513 | |
* @see https://stackoverflow.com/a/33824164/230513 | |
*/ | |
public class WebViewPane extends Application { | |
private static final String SITE = "https://www.example.com"; | |
private final WebView webView = new WebView(); | |
private enum Site { | |
HOME("Home", SITE), | |
BING("Bing", "https://www.bing.com"), | |
DUCK("Duck", "https://www.duckgo.com"), | |
GOOGLE("Google", "https://www.google.com"), | |
JAVAFX("JavaFX", "https://stackoverflow.com/tags/javafx"), | |
YAHOO("Yahoo", "https://www.yahoo.com"); | |
private final KeyCombination k; | |
private final String name; | |
private final String url; | |
private Button button; | |
private Site(String name, String url) { | |
int n = this.ordinal() + 1; | |
this.k = KeyCombination.keyCombination("Shortcut+" + n); | |
this.name = k.getDisplayText() + " " + name; | |
this.url = url; | |
} | |
private static void addAccelerators(Scene scene) { | |
for (Site site : values()) { | |
scene.getAccelerators().put(site.k, (Runnable) () -> { | |
site.button.fire(); | |
}); | |
} | |
} | |
} | |
private void load(String site) { | |
webView.getEngine().load(site); | |
} | |
private void back() { | |
final WebHistory history = webView.getEngine().getHistory(); | |
ObservableList<WebHistory.Entry> entryList = history.getEntries(); | |
int currentIndex = history.getCurrentIndex(); | |
Platform.runLater(() -> { | |
history.go(entryList.size() > 1 | |
&& currentIndex > 0 ? -1 : 0); | |
}); | |
} | |
@Override | |
public void start(Stage stage) { | |
stage.setTitle("Home"); | |
WebEngine webEngine = webView.getEngine(); | |
webEngine.load(Site.HOME.url); | |
BorderPane page = new BorderPane(webView); | |
ToolBar toolBar = new ToolBar(); | |
for (Site site : Site.values()) { | |
site.button = new Button(site.name); | |
site.button.setOnAction((ActionEvent e) -> { | |
load(site.url); | |
}); | |
toolBar.getItems().add(site.button); | |
} | |
KeyCombination bKey = KeyCombination.keyCombination("Shortcut+["); | |
Button back = new Button(bKey.getDisplayText()); | |
back.setOnAction((ActionEvent e) -> { | |
back(); | |
}); | |
Button exit = new Button("Exit"); | |
exit.setOnAction((ActionEvent e) -> { | |
Platform.exit(); | |
}); | |
TextField url = new TextField(); | |
url.setPrefWidth(256); | |
url.setPromptText("Enter a URL & click the \"Go\" button."); | |
url.setOnAction((e) -> { | |
load(url.getText()); | |
}); | |
Button go = new Button("Go"); | |
go.setOnAction((e) -> { | |
load(url.getText()); | |
}); | |
toolBar.getItems().addAll(back, exit, url, go); | |
page.setOnKeyPressed((KeyEvent e) -> { | |
KeyCode code = e.getCode(); | |
if (KeyCode.END == code) { | |
Platform.exit(); | |
} else if (KeyCode.HOME == code) { | |
load(Site.HOME.url); | |
} | |
}); | |
page.setBottom(toolBar); | |
Scene scene = new Scene(page); | |
Site.addAccelerators(scene); | |
scene.getAccelerators().put(bKey, this::back); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
public static void main(String[] args) { | |
Application.launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment