How to load a local HTML file using WebView in JavaFX - java

I am looking for a way to display an html file in a different stage once the help button is clicked.
public void handleButtonAction(ActionEvent event) throws IOException {
if (event.getSource() == help) {
stage = (Stage) help.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("help.fxml"));
WebView browser = new WebView();
Scene helpScene = new Scene(root);
Stage helpStage = new Stage();
helpStage.setTitle("Help Menu");
helpStage.setScene(helpScene);
URL url = getClass().getResource("readme.html");
browser.getEngine().load(url.toExternalForm());
helpStage.show();
}
}

Your code is fine except that you forgot to add the webview to the scene, do
((Pane) helpScene.getRoot()).getChildren().add(browser);

Related

Watch Twitch Live Stream in JavaFX

I want to do a simple JavaFX application to watch Twitch Live Streams.
So far, I come up with 3 possible solutions:
1) JavaFX WebView. With it I was successful to watch Twitch clip video, but Live Steaming seems not supported.
Code:
public class App extends Application {
public static void main(String[] args) {
launch();
}
#Override
public void start(Stage stage) {
WebView webview = new WebView();
webview.getEngine().load(
"https://www.twitch.tv/stariy_bog/clip/RelievedPopularYakinikuDancingBanana"
);
WebView webView = new WebView();
webview.setPrefSize(640, 390);
stage.setScene(new Scene(webview));
stage.show();
}
}
2) JavaFX scene.media package. I’ve found this package in JavaFX documentation: it states that HLS Live Streaming is supported, but I’ve found no success in loading any Twitch channel.
Code:
public class MediaTest extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
String uri = "https://www.twitch.tv/qsnake";
try {
primaryStage.setTitle("Embedded Media Player");
Group root = new Group();
Scene scene = new Scene(root, 540, 210);
Media media = new Media(uri);
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
// create mediaView and add media player to the viewer
MediaView mediaView = new MediaView(mediaPlayer);
((Group) scene.getRoot()).getChildren().add(mediaView);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
mediaPlayer.setOnPlaying(() -> {
System.out.println(mediaPlayer.getMedia().getDuration().toString());
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3) Twitch Developers API. Did not found any information about live steams using Java here so far.
Also, yes, I’ve searched web about this problem. For example I’ve found a topic, but it OPs problem was not resolved.

How to reset opacity of Pane?

I'm a newbie in JavaFX. I made a fx application it has a home and another jfxml file.
This is HomeContoler.java file for open another jfxmlfile
#FXML
public void actionIngredencesReg(ActionEvent event) {
try {
mainHome.setOpacity(0.2);
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Ingrdences.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.UNDECORATED);
Scene scene = new Scene(root1);
stage.setScene(scene);
stage.show();
// mainHome.setOpacity(1);
} catch (Exception e) {
e.printStackTrace();
}
}
when I click on the menu items it will works likes
this the close code of Ingredients.fxml file
#FXML
Label close_label;
this is a label, here in action
#Override
public void initialize(URL url, ResourceBundle rb) {
close_label.setOnMouseClicked(e -> {
//this is code for close only science
Stage stage = (Stage) close_label.getScene().getWindow();
stage.close();
});
}
but after close Ingredients.fxml file home.fxml file be like this
[this is not needed for me][4]
I want to convert home.fxml file like this
as like this after closing the ingredient.fxml
i want to setOpcaity of home.fxml file into 1 after close the ingredient.fxml file
anyone can help me to fix it...
Just revert the opacity when the stage is hidden:
stage.setOnHidden(e -> mainHome.setOpacity(1));

How to add JavaFX Preloader ProgressBar on top of an FXML scene

I am new to Java and JavaFX all together, so please be patient with me if I am asking the wrong question here.
I am trying to add preloader Scene to an application I built: I was able to add the preloader using the code below. It's the default preloader and it looks very basic. So, here is my question. 1) how to add percentage progress status instead of progressbar. 2) Is it possible to load this progress bar on top of an FXML scene
Preloader
public class JavaFXPreloader3 extends Preloader {
ProgressBar bar;
Stage stage;
private Scene createPreloaderScene() throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("FXML.fxml"));
ProgressIndicator pi = new ProgressIndicator();
BorderPane p = new BorderPane();
p.setCenter(pi);
return new Scene(p, 300, 150);
}
#Override
public void start(Stage stage) throws Exception {
this.stage = stage;
stage.setScene(createPreloaderScene());
stage.show();
}
#Override
public void handleStateChangeNotification(StateChangeNotification scn) {
if (scn.getType() == StateChangeNotification.Type.BEFORE_START) {
stage.hide();
}
}
#Override
public void handleProgressNotification(ProgressNotification pn) {
bar.setProgress(pn.getProgress());
}
}
How do I return root scene instead of the BorderPane p including the Progress Indicator:
ProgressIndicator pi = new ProgressIndicator();
BorderPane p = new BorderPane();
p.setCenter(pi);
return new Scene(p, 300, 150);
This is a excellent tutorial for creating a PreLoader.
https://docs.oracle.com/javafx/2/deployment/preloaders.htm

Passing a global ObservableList between 2 classes

I have a question. How can i pass the same reference of one ObservableList between two classes? I need to add an Element from my SimultanTestController to the List in FXMLDocumentController.
My Code (FXMLDocumentController):
public class FXMLDocumentController implements Initializable {
Stage primaryStage = new Stage();
Stage options = new Stage();
Stage selectTest = new Stage();
ObservableList<DefaultTestInterface> items = FXCollections.observableArrayList();
public ObservableList<DefaultTestInterface> getList() {
return items;
}
#FXML
private ListView ItemList;
#FXML
public Button btn_globalOptions;
#FXML
private Button btn_add;
#FXML
private Button btn_delete;
#FXML
private Button btn_moveUP;
#FXML
private Button btn_moveDOWN;
public void getPrimaryStage(Stage stage){
this.primaryStage=stage;
}
My Code (SimultanTestController):
public class SimultanTestController implements Initializable {
ObservableList<DefaultTestInterface> items = (new FXMLDocumentController()).getList();
#FXML
Button btn_ok;
public void addTest(ActionEvent event){
for( int x = 0 ; x < items.size() ; x++ ) { // start from index 0
System.out.println("Item" +items.get(x));
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
btn_ok.setOnAction(this::addTest);
}
}
So you see, i have one Method: getList
and this method is called in SimultanTestController and i try to parse the value of getList to the ObservableList in SimultanTestController.
After that, i want to to add an element to the ObservableList in FXMLDocumentController from SimultanTestController. But i dont get the same list in both classes ...
Thank you for help!
I load my FXML-Files in this class:
First i create all new stages for my FXML-Files and have 3 ObservableList with the FXML-File-Names, the window-titel and all the stages. Then i load all stages with my for-loop. (By the way, is this a good solution?)
My window opening way: Main-->SelectTest-->SimultanTest
public class SelectTestController implements Initializable {
ObservableList<String> items =FXCollections.observableArrayList();
Stage primaryStage = new Stage();
Stage simultan = new Stage();
Stage audioLoop = new Stage();
Stage cpu = new Stage();
Stage driversCD = new Stage();
Stage driversHD = new Stage();
Stage driversFD = new Stage();
Stage externalProgramm = new Stage();
Stage graphics = new Stage();
Stage interactive = new Stage();
Stage mainboard = new Stage();
Stage memorySPD = new Stage();
Stage memory = new Stage();
Stage network = new Stage();
Stage ports = new Stage();
Stage reboot = new Stage();
Stage signal = new Stage();
Stage userMessage = new Stage();
#FXML
private ListView lv_selectTest;
#FXML
private Button btn_choose;
#FXML
private Label test;
public void getPrimaryStage(Stage stage){
this.primaryStage=stage;
}
ObservableList<Stage> stages =FXCollections.observableArrayList();
ObservableList<String> fxml =FXCollections.observableArrayList();
ObservableList<String> stageTitel =FXCollections.observableArrayList();
public void initWindows(){
try {
for(int i=0;i<=stages.size();i++){
Window parent = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource(fxml.get(i)));
Scene scene = new Scene(root);
stages.get(i).initOwner(parent);
stages.get(i).initModality(Modality.APPLICATION_MODAL);
stages.get(i).setTitle(stageTitel.get(i));
stages.get(i).setScene(scene);
stages.get(i).setResizable(false);
stages.get(i).setAlwaysOnTop(true);
}
}catch (IOException|IllegalStateException ex) {
System.out.println(ex);
}
}
I tried to get the controller of the FXML-Loader and this works. I get the value from JavaFX-Start methode and parse it to FXMLDocumentController. Now i have a var with the controller instance in my FXMLDocumentConroller. But i am not able to get the value in SimultanTest ot add something to the ObservableList items. I tired per new Instance() and setter+getter... I dont know why i dont get it...
It would be very nice if someone could give me an example based of my code ... You see i worked for my solution but im not getting further...
i found an own solution for my Problem. I dont get the Controller of FXMLMain, but i solved it with an Singelton Class which stores the ObservableList items.
Now i can set Items from every class and can show them in ListView in FXMLDocumentController. If some one have the same Problem, connect me for a solution.
Thanks for help # James_D

JavaFX WebView: Java not accessible

I am trying to load a webpage into my application. I can get the page to load, however, the website I'm trying to load requires Java to run and the WebView cannot seem to find it on my MacBook. See my stripped code below:
public class LoadPage extends Application {
public static WebView loadPage() {
WebView browser = new WebView();
WebEngine eng = browser.getEngine();
eng.load("http://www.java.com/en/download/installed.jsp");
return browser;
}
#Override
public void start(Stage primaryStage) {
try {
BorderPane pane = new BorderPane();
pane.setCenter(loadPage());
Scene scene = new Scene(pane);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("WebEditor");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Now my question is: Is there a workaround for this? Any help is much appreciated :)
No you cannot do that, the WebView currently doesn't support plugin technology ! Please see Jewelsea's answer over here !
Javafx: Java applet in a Webview component

Categories