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
Related
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.
Im building an application that shows a window that ask the user if he want to suspend the computer with two button options, one of them its a YES and the PC suspends.
The other button named "Later" supposed to hide the window and after an hour it appears again and ask the same question.
Code for the "later buttton"
noButton.setOnAction(event -> {
Done=false; //boolean to close and open the frame
Gui gui = new Gui();
try {
gui.start(classStage);
} catch (Exception e) {
e.printStackTrace();
}
});
The boolean that you see in the code is bc it was the way i think i could control that, trust me i tried in different ways but no one just help me with the issue, here is the code of the GUI class
public class Gui extends Application {
public Stage classStage = new Stage();
public static boolean Done=true;
public static boolean flag=true;
public Gui() {
}
#Override
public void start(Stage primaryStage) throws Exception {
Done = Controller.isDone();
classStage = primaryStage;
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
primaryStage.setX(primaryScreenBounds.getMaxX() - primaryScreenBounds.getWidth());
primaryStage.setY(primaryScreenBounds.getMaxY() - primaryScreenBounds.getHeight());
primaryStage.setAlwaysOnTop(true);
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
primaryStage.setTitle("Alerta suspencion de equipo");
primaryStage.setScene(new Scene(root));
primaryStage.setResizable(false);
primaryStage.initStyle(StageStyle.UNDECORATED);
if (Controller.isDone() == true) {
primaryStage.show();
} else if(Controller.isDone() == false) {
primaryStage.hide();
Platform.exit(); // this is the only way that the windows close
}
}
i know that Platform.exit(); kills the program but when i only use .hide(); of the Stage nothing happens, the window never closed, the worst part is that when i use the Platform.exit() command i cant make the frame appear again...
Anyone knows a way maybe easier to hide and show a window after certain time? maybe im doing this wrong.
Regards.
It's not really clear what's going on in the code in your question. The bottom line is that you should never create an instance of Application yourself; the only Application instance should be the one created for you.
I don't actually see any need to have a separate class for the functionality you've shown (though you could, of course). All you need to do is hide classStage if the no button is pressed, and open it again in an hour:
noButton.setOnAction(event -> {
Done=false; //boolean to close and open the frame
classStage.hide();
PauseTransition oneHourPause = new PauseTransition(Duration.hours(1));
oneHourPause.setOnFinished(e -> showUI(classStage));
oneHourPause.play();
});
// ...
private void showUI(Stage stage) {
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
stage.setX(primaryScreenBounds.getMaxX() - primaryScreenBounds.getWidth());
stage.setY(primaryScreenBounds.getMaxY() - primaryScreenBounds.getHeight());
stage.setAlwaysOnTop(true);
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
stage.setTitle("Alerta suspencion de equipo");
stage.setScene(new Scene(root));
stage.setResizable(false);
stage.initStyle(StageStyle.UNDECORATED);
stage.show();
}
Note that the FX Application will exit if the last window is closed, by default. So you should call Platform.setImplicitExit(false); in your init() or start() method.
I am assuming you are reloading the FXML file because the UI might have changed since it was previously loaded. Obviously if that's not the case, all you have to do is show the stage again as it is:
noButton.setOnAction(event -> {
Done=false; //boolean to close and open the frame
classStage.hide();
PauseTransition oneHourPause = new PauseTransition(Duration.hours(1));
oneHourPause.setOnFinished(e -> classStage.show());
oneHourPause.play();
});
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
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);
I've been using JavaFx lately, as a beginner, and have been very impressed. At the moment I'm stuck trying to set a pagination slideshow to automatically
move the slideshow forward every 5 seconds (and back to the first slide to continue when the last slide is reached). Can any one steer me in the right direction here?
#FXML
public void slideshow(ActionEvent event) {
// TODO Auto-generated method stub
String[] photos = { "housestark.jpg", "housefrey.jpg", "housebar.jpg",
"HouseBolton.jpg", "housegreyjoy.jpg", "houseaaryn.jpg",
"houselannis.jpg", "housemart.jpg", "housereed.jpg",
"housetully.jpg", "housetyrel.jpg", };
Pagination p = new Pagination(photos.length);
p.setPageFactory((Integer pageIndex) -> {
return new ImageView(getClass().getResource(photos[pageIndex])
.toExternalForm());
});
Stage stage = new Stage();
stage.setScene(new Scene(p));
stage.setX(1250);
stage.setY(10);
stage.setTitle("Slideshow");
stage.setResizable(false);
stage.show();
}
This is my code so far! I would appreciate any help anyone could give?
It's pretty easy. All you have to do is create a timer that runs every 5 seconds, and when it runs move the page index.
public class SO extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Pagination p = new Pagination(10);
Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(5), event -> {
int pos = (p.getCurrentPageIndex()+1) % p.getPageCount();
p.setCurrentPageIndex(pos);
}));
fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);
fiveSecondsWonder.play();
stage.setScene(new Scene(p));
stage.show();
}
}
the five second wonder came from here: JavaFX periodic background task