Ive been trying to get WebView working on my javafx app, but I dont know how.
package com.adventureg147.mclauncher;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import java.io.IOException;
import java.net.URL;
import java.util.EventObject;
import java.util.ResourceBundle;
public class Controller implements Initializable{
@FXML
private WebView webView;
private WebEngine engine;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
engine = webView.getEngine();
loadPage();
}
public void loadPage() {
engine.load("https://www.google.com/");
}
@FXML
private Stage stage;
private Scene scene;
private Parent root;
public void switchToMP(ActionEvent event) throws IOException {
root = FXMLLoader.load(getClass().getResource("MPMenu.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void switchToShop(ActionEvent event) throws IOException {
root = FXMLLoader.load(getClass().getResource("ShopMenu.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void switchToNews(ActionEvent event) throws IOException {
root = FXMLLoader.load(getClass().getResource("NewsMenu.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
The block of code above has two different parts basically;
-One to switch scenes
-The other for the web view
I dont even know if I’m meant to specify a scene or anything, I’m kinda new to this.
Here is the fxml for the scene im trying to add webview to btw
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.web.WebView?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1250.0" style="-fx-background-color: RoyalBlue;" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.adventureg147.mclauncher.Controller">
<children>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="90.0" stroke="TRANSPARENT" strokeType="INSIDE" width="1250.0" />
<Button layoutX="87.0" layoutY="1.0" mnemonicParsing="false" onAction="#switchToMP" prefHeight="90.0" prefWidth="250.0" style="-fx-background-color: dodgerblue;" text="MODPACKS">
<font>
<Font name="Lato Light" size="32.0" />
</font>
</Button>
<Button layoutX="472.0" mnemonicParsing="false" onAction="#switchToNews" prefHeight="90.0" prefWidth="135.0" style="-fx-background-color: #1177da;" text="NEWS">
<font>
<Font name="Lato Light" size="32.0" />
</font>
</Button>
<Button layoutX="337.0" mnemonicParsing="false" onAction="#switchToShop" prefHeight="90.0" prefWidth="135.0" style="-fx-background-color: dodgerblue;" text="SHOP">
<font>
<Font name="Lato Light" size="32.0" />
</font>
</Button>
<ImageView fitHeight="87.0" fitWidth="87.0" layoutY="2.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../icon.png" />
</image>
</ImageView>
<WebView fx:id="webView" layoutY="89.0" prefHeight="712.0" prefWidth="1250.0" />
</children>
</AnchorPane>
I tried to “specify” the scene by copying and pasting one of the “switchToX” things, that didnt work.
AdventureG147 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.