How can i create a stage in java FX which forces user to first close it for doing anything else? - javafx

I am creating a desktop app using java fx and here I want to prevent the user from accessing the PC unless my app is closed.
I tried using showAndWait() function but it is not applicable to primary stage.
Its a single page app.
Help will be appreciated. Thanks!
Here is my main class and i am using scene builder for designing.
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
TilePane root = FXMLLoader.load(getClass().getResource("/application/Layout.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.showAndWait();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}

Related

live video streaming from url in javafx

I want to show live streaming video from url in javafx. I am using webview.
It is showing only text in that webpage. It is not showing live video streaming. Please suggest me a way to do that.
public class A extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
WebView webView = new WebView();
webView.getEngine().load("http://192.168.0.143:8000/");
VBox vbox = new VBox(webView);
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

How to call the public void start(Stage primaryStage) Method from a different class

I'am new to JavaFX, so my coding is not the best. My programme has one Stage with 2 different Scenes. For a better overview I have created a new class for the second Scene. From this second Scene I want to go back to first one, using a button. The method: primaryStage.setScene(scene) is not working and I dont know why. Can someone please help me? :)
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
GridPane primarygridpane = new GridPane();
Scene scene = new Scene(primarygridpane,400,400);
...
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
public class Harnwegsinfektion {
public static void create (Stage primaryStage){
GridPane secondarygridpane = new GridPane();
Scene scene2 = new Scene(secondarygridpane,400,400);
buttonBack.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(scene);
}
});
primaryStage.setScene(scene2);
primaryStage.show();
}
}
The problem is that your scene variable is named scene2 and you are calling setScene() with scene.
I don't really know when and where create() is called, but if you want to go back to the first scene from the button, you have to save the original one before switching :
public static void create (Stage primaryStage){
GridPane secondarygridpane = new GridPane();
Scene scene2 = new Scene(secondarygridpane,400,400);
Scene originalScene=primaryStage.getScene(); //save the previous scene
buttonBack.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(originalScene); //go back to the previous scene
}
});
primaryStage.setScene(scene2);
primaryStage.show();
}

Add KeyEvent for calculator numpad in JavaFX using MVC

I created in Eclipse a simple calculator using JavaFx and MVC pattern. I would like to add keylisteners in order to press the buttons of my calculator by simply pressing the buttons in my keyboard. I tried to add #onKeyPress in SceneBuilder and then a method onKeypress (with some coding inside) in my Controller class but nothing happens.Could you please give some general instructions how to implement something like this? Thanks!
Thanks for your comments. I added the following code snippet in App.java:
scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
controller.numFromKeyboard(event.getCode().toString());
}
});
And also, I had to add:
Parent root = loader.load();
Controller controller = loader.getController();
// The above line MUST be
// inserted after root is loaded in order the controller of my
// app to be instantiated,
// otherwise we will get a null exception when handler will be
// invoked
App.java
public class App extends Application {
//controller = new Controller();
#Override
public void start(Stage primaryStage) {
try {
// Read file fxml and draw interface.
FXMLLoader loader = new FXMLLoader(getClass()
.getResource("/application/View.fxml"));
Parent root = loader.load();
Controller controller = loader.getController();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("/application/application.css").toExternalForm());
Image icon = new Image(getClass().getResourceAsStream("/application/Assets/App.png"));
primaryStage.getIcons().add(icon);
primaryStage.setTitle("JavaFX Calculator by Dimitris Baltas");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
controller.numFromKeyboard(event.getCode().toString());
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

How to create splash screen as a Preloader in JavaFX standalone application?

I created a Preloader (based on the following tutorial) that should display a splash screen for the main application.
9.3.4 Using a Preloader to Display the Application Initialization Progress
http://docs.oracle.com/javafx/2/deployment/preloaders.htm
public class SplashScreenLoader extends Preloader {
private Stage splashScreen;
#Override
public void start(Stage stage) throws Exception {
splashScreen = stage;
splashScreen.setScene(createScene());
splashScreen.show();
}
public Scene createScene() {
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 200);
return scene;
}
#Override
public void handleApplicationNotification(PreloaderNotification notification) {
if (notification instanceof StateChangeNotification) {
splashScreen.hide();
}
}
}
I'd like to run preloader each time I run the main application in my IDE (IntelliJ IDEA).
I also followed the packaging rules for preloaders in IntelliJ:
https://www.jetbrains.com/idea/help/applications-with-a-preloader-project-organization-and-packaging.html
When I run the main application the preloader doesn't start, so I suppose I'm missing something. I'm new to Preloaders and I don't understand what is the mechanism to connect the main app with the preloader in standalone application.
You can run using LauncherImpl like this . . .
public class Main {
public static void main(String[] args) {
LauncherImpl.launchApplication(MyApplication.class, SplashScreenLoader.class, args);
}
}
And the class MyApplication would be like this . . .
public class MyApplication extends Application {
#Override
public void start(Stage primaryStage) {
....
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The IDEs aren't great at adding preloaders yet. Take a look at the Manifest in your program's jar file and make sure this line is present:
JavaFX-Preloader-Class: SplashScreenLoader
May be too late, this can also help somebody.
For me, i used JavaFX service and task to create splash screen as a Preloader in JavaFX standalone application. This, because the contexte of my project.
Create the AnchorPane and the progress Pane
#FXML
private AnchorPane anchorPane;
private MaskerPane progressPane;
public static void main(String[] args) {
launch(args);
}
#Override
public void init() throws Exception {
progressPane = new MaskerPane();
progressPane.setText(bundle.getString("root.pleaseWait"));
progressPane.setVisible(false);
AnchorPane.setLeftAnchor(progressPane, 0.0);
AnchorPane.setTopAnchor(progressPane, 0.0);
AnchorPane.setRightAnchor(progressPane, 0.0);
AnchorPane.setBottomAnchor(progressPane, 0.0);
anchorPane.getChildren().add(progressPane);
}
#Override
public void start(Stage initStage) {
//.....
initRoot();
//.....
}
Create the splash screen service as this:
private final Service<Void> splashService = new Service<Void>() {
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
public Void call() throws Exception {
//main code, the code who take time
//or
//Thread.sleep(10000);
return null;
}
};
}
};
Start service and show/hide the progressPane on initRoot when loading the main screen:
public void initRoot() {
try {
//....
splashService.restart();
//On succeeded, do this
splashService.setOnRunning(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent event) {
//Show mask on succeed
showMask(Boolean.TRUE);
}
});
splashService.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent event) {
splashService.cancel();
//Hide mask on succeed
showMask(Boolean.FALSE);
}
});
//.....
primaryStage.show();
} catch (IOException ex) {
ex.printStackTrace();
}
}
To show/hide the progress...
showMask(boolean value){
progressPane.setVisible(value);
};

JavaFX preloader not updating progress

I'm having trouble with the JavaFX Preloader. During the start phase the application will have to connect to a DB and read many so I thought it would be nice to display a splash screen during this time. The problem is the ProgressBar automaticly goes to 100% and I don't understand why.
Application class. Thread sleep will be replaced by real code later (DB connection etc)
public void init() throws InterruptedException
{
notifyPreloader(new Preloader.ProgressNotification(0.0));
Thread.sleep(5000);
notifyPreloader(new Preloader.ProgressNotification(0.1));
Thread.sleep(5000);
notifyPreloader(new Preloader.ProgressNotification(0.2));
}
Preloader
public class PreloaderDemo extends Preloader {
ProgressBar bar;
Stage stage;
private Scene createPreloaderScene() {
bar = new ProgressBar();
bar.getProgress();
BorderPane p = new BorderPane();
p.setCenter(bar);
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());
System.out.println("Progress " + bar.getProgress());
}
For some reason I get the following output:
Progress 0.0
Progress 1.0
I had same problem and I found solution after two hours of searching and 5 minutes of carefully reading of JavaDoc.:)
Notifications send by notifyPreloader() method can be handled only by Preloader.handleApplicationNotification() method and it doesn't matter which type of notification are you sending.
So change you code like this:
public class PreloaderDemo extends Preloader {
.... everything like it was and add this ...
#Override
public void handleApplicationNotification(PreloaderNotification arg0) {
if (arg0 instanceof ProgressNotification) {
ProgressNotification pn= (ProgressNotification) arg0;
bar.setProgress(pn.getProgress());
System.out.println("Progress " + bar.getProgress());
}
}
}

Resources