Concurrency in Java(FX) - java

In my JavaFX application I want to display an intermediate ProgressIndicator, while loading data from a database for instance. I tried to solve this problem by setting a different fxml-file (indicator.fxml), containing a ProgressIndicator with intermediate property, into the main BorderPane. And after that, setting the desired Stage into the BorderPane.
The indicator.fxml gets displayed, but seems to be frozen, because it is not animating. PersondataController controller = loader.getController() takes some time to be finished.
#FXML
private void showPersonData(){
try{
FXMLLoader loader2 = new FXMLLoader();
loader2.setLocation(RootLayoutController.class.getResource("Indicator.fxml"));
AnchorPane progress = loader2.load();
mainApp.getRootLayout().setCenter(progress);ยด
Platform.runLater(new Runnable() {
#Override
public void run() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(RootLayoutController.class.getResource("Persondata.fxml"));
PersondataController controller = loader.getController();
AnchorPane page = null;
try {
page = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
mainApp.getRootLayout().setCenter(page);
}
});
}catch(Exception e){
e.printStackTrace();
}
Any ideas how I can solve this problem?

Finally solved my problem with your help. thank you guys.
Instead of the Platform.runLater() stuff I did:
Service<Void> backgroundThread = new Service<Void>() {
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
FXMLLoader loader2 = new FXMLLoader();
loader2.setLocation(RootLayoutController.class.getResource("Persondata.fxml"));
PersondataController = loader2.getController();
page = loader2.load();
return null;
}
};
}
};
backgroundThread.setOnSucceeded((evt) -> {
mainApp.getRootLayout().setCenter(page);
});
backgroundThread.start();

Related

JavaFX : Use an Indeterminate Progressbar in a splashScreen

I have a splash screen :
I need to have the animation of the progress bar (Indeterminate) but it doesn't work.
It's maybe due to because my thread is running in my initilize methode.
public class splashscreenController implements Initializable {
#Override
public void initialize(URL location, ResourceBundle resources) {
new SplashScreen().run();
}
class SplashScreen extends Task {
#Override
public Object call() {
Platform.runLater(new Runnable() {
#Override
public void run()
Parent root = null;
try {
Thread.sleep(3000);
root = FXMLLoader.load(getClass().getResource("../gui/NewUI.fxml"));
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
assert root != null;
Scene scene = new Scene(root, 1280, 720);
stage.setScene(scene);
stage.show();
MainJavaFx.setPrimaryStage(stage);
((Stage) panParent.getScene().getWindow()).close();
}
});
return null;
}
}
}
There are 2 issues in your code:
new SplashScreen().run();
A Task does not provide functionality for running on a new thread. run is executed on the calling thread.
class SplashScreen extends Task {
#Override
public Object call() {
Platform.runLater(new Runnable() {
#Override
public void run() {
// placeholder for parts of your code
longRunningOperation();
guiUpdate();
}
});
return null;
}
}
Even if you execute this task on a seperate thread, the Runnable passed to Platfrom.runLater is executed on the JavaFX application thread and doing long-running operations from this runnable freezes the GUI.
Do all the long-running operations on the background thread instead and only do short updates using Platfrom.runLater.
new Thread(new SplashScreen()).start();
class SplashScreen extends Task {
#Override
public Object call() throws IOException, InterruptedException {
Thread.sleep(3000);
final Parent root = FXMLLoader.load(getClass().getResource("../gui/NewUI.fxml"));
Platform.runLater(new Runnable() {
#Override
public void run() {
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
Scene scene = new Scene(root, 1280, 720);
stage.setScene(scene);
stage.show();
MainJavaFx.setPrimaryStage(stage);
((Stage) panParent.getScene().getWindow()).close();
}
});
return null;
}
}
Note that since you're not using the functionality provided by Task, you could simply implement Runnable with your class instead of inheriting from Task.

Stage is showing to be null in javafx

This method(abc) is called when a button is pressed in the first scene. What it does is it changes the scene to waitingScreen and calls another method waitscr()
public void abc(ActionEvent event)throws Exception{
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
//for changing the scene.
Parent administrator =
FXMLLoader.load(getClass().getResource("waitingScreen.fxml"));
stage.setScene(new Scene(administrator));
stage.show();
conn.close();
waiting_screen_Controller c = new waiting_screen_Controller();
c.waitscr(event);
What waitscr does is it starts a timer for 5 seconds and when the timer ends
it calls another method setscr() (maybe i could have started the timer in abc only)
public void waitscr(ActionEvent event)throws IOException{
timetask = new TimerTask(){
#Override
public void run() {
if(!timing){
try{
timetask.cancel();
setscr(event);
}
catch(Exception ex){
ex.printStackTrace();
}
}
else
timing = updateTime();
}
};
timer.scheduleAtFixedRate(timetask,1000,1000);
}
it updates the time
public boolean updateTime(){
System.out.println(s);
if(s==0){
return false;
}
s--;
return true;
}
what setscr does is it changes the scene back to the first one..
public void setscr(ActionEvent event)throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("first.fxml"));
Parent parent = loader.load();
Scene s=new Scene(parent);
stage = (Stage)((Node) event.getSource()).getScene().getWindow();
System.out.print(event.getSource());
stage.setScene(s);
stage.show();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
But the problem is it gives npe in stage.
java.lang.NullPointerException
at sample.waiting_screen_Controller.setscr(waiting_screen_Controller.java:106)
at sample.waiting_screen_Controller$1.run(waiting_screen_Controller.java:45)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
I thought this was because of ActionEvent because npe is at stage but i printed the source of ActionEvent and it is not null.
You're replacing the scene before calling waitscr. This way when you call Scene.getWindow the scene is no longer associated with a window and the result is null.
You shouldn't do this from a non-application thread anyways.
By retrieving the window only once and using a Platform.runLater you should be able to fix this issue:
public void abc(ActionEvent event)throws Exception{
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
...
c.waitscr(stage);
public void waitscr(final Stage stage) throws IOException {
timetask = new TimerTask(){
#Override
public void run() {
if(!timing){
try{
timetask.cancel();
setscr(stage);
} catch(Exception ex){
ex.printStackTrace();
}
}
else
timing = updateTime();
}
};
timer.scheduleAtFixedRate(timetask,1000,1000);
}
public void setscr(Stage stage)throws IOException{
// there seems to be a try missing somewhere
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("first.fxml"));
Parent parent = loader.load();
Scene s=new Scene(parent);
Platform.runLater(() -> {
// scene update on javafx application thread
stage.setScene(s);
stage.show();
});
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}

JavaFX and ProgressIndicator with fxml

I wonder from morning how to solve the problem. I have login aplication. When user waiting for login i want use processindicator. I used the second thread but it does not work
Main loader fxml
MainController
#FXML public StackPane MainStackPane;
public void initialize(URL arg0, ResourceBundle arg1) {
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/LoginForm/Login.fxml"));
Pane pane = null;
try {
pane = loader.load();
} catch (IOException e) {System.out.println(e.getMessage());}
LoginController login = loader.getController();
login.setMainController(this);
setScreen(pane, true);
}
public void setScreen(Pane pane, boolean clean){
MainStackPane.getChildren().addAll(pane);
}
LoginForm:
private MainController mainController;
private void Zaloguj() throws IOException {
String cryptUser = null, cryptPass = null;
Test test = new Test(this.mainController);
test.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
LoginSQL sql = new LoginSQL();`
Byte LoginResult = sql.SQLselect(cryptUser, cryptPass);
...}
Class Test
public class test extends Service<Void>{
private MainController mainController;
public test(MainController mainController) {
this.mainController = mainController;
}
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
System.out.println("Service: START");
ProgressIndicator p = new ProgressIndicator();
Platform.runLater(new Runnable() {
#Override public void run() {
mainController.MainStackPane.getChildren().addAll(p);
}});
if (isCancelled()) {
mainController.MainStackPane.getChildren().remove(p);
}
return null;
};};}}
ProgressIndicator appears only in the next window after login. How to do it ?
JavaFX rendering happens in the main thread only. If you add the ProgressIndicator and then use Thread.sleep(), JavaFX won't render the indicator until Thread.sleep() is done. Also, if the login request hangs, JavaFX will also wait until the login request is complete before rendering.
What you have to do is to make sure to never interrupt/hang the main thread. Remove Thread.sleep, and also move your login request to a child thread. When the request is complete, notify the main thread so that it can remove the ProgressIndicator.

How to rerun a program in JavaFX

I have 2 controller for 2 fxml files. In one controller I have a handleOpen function which opens a file chooser and gives the path to a Class which I have called model. Then on the other controller a function, treeTableDraw gets this path, after clicking on Draw Button and runs the program. I have Another Button to reset the program. It sets the result back to null, but when open another file to run, the program crashes, because the path is null. How can I reset the program and make it use the new path which is selected from open file chooser?
//Gets the path from model and runs the program
public void treeTableDraw(ActionEvent event) {
new Controller(model.getText());
drawTable();
numberOfFunctions = dc.getFuncAll().size();
numberOfOrganizations = dc.getSortedAssignedOrg().size();
funcLabel.setText(numberOfFunctions + "");
orgLabel.setText(numberOfOrganizations + "");
btnDraw.setDisable(true);
}
/**
* Clrears TreeTableView and sets back labels
*
* #param event
*/
public void treeTableReset(ActionEvent event) {
btnDraw.setDisable(false);
model.setText(null);
funcLabel.setText("0");
orgLabel.setText("0");
treeTable.getColumns().clear();
}
This is RootLayout class which has open file function:
#FXML
private void handleOpen() {
FileChooser fileChooser = new FileChooser();
// Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
"3lgm2 files (*.z3lgm)", "*z3lgm");
fileChooser.getExtensionFilters().add(extFilter);
// Show open file dialog
File file = fileChooser.showOpenDialog(main.getPrimaryStage());
if (file != null) {
path = file.toString();
model.setText(path);
}
}
Here is the model class
public class Model {
private final StringProperty text = new SimpleStringProperty();
public StringProperty textProperty() {
return text;
}
public final String getText() {
return textProperty().get();
}
public final void setText(String text) {
textProperty().set(text);
}
}
This is the main, where I combine two fxmls and set stage:
public class Main extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private Model model = new Model();
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("IT-Saturation");
initRootLayout();
showOverView();
}
private void showOverView() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/view/OverView.fxml"));
loader.setController(new OverViewController(model));
AnchorPane overView = (AnchorPane) loader.load();
rootLayout.setCenter(overView);
} catch (IOException e) {
e.printStackTrace();
}
}
private void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/view/RootLayout.fxml"));
loader.setController(new RootLayoutController(model));
rootLayout = (BorderPane) loader.load();
// show scene containing the root layout
Scene scene = new Scene(rootLayout);
scene.getStylesheets().add(
getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
// gives controller access to main
RootLayoutController controller = loader.getController();
controller.setMainApp(this);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
*
* #return primaryStage
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
public void showMostComputerizedStatistics() {
try {
// Load the fxml file and create a new stage for the popup.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class
.getResource("view/BirthdayStatistics.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Birthday Statistics");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set the persons into the controller.
MostComputerizedController controller = loader.getController();
dialogStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The problem was not the path. I had to reset the data which got initialized at the the former run of program. So after setting path to null I just newed the instance of class which had reference to data.
...
public void treeTableReset(ActionEvent event) {
btnDraw.setDisable(false);
//model.setText(null);
funcLabel.setText("0");
orgLabel.setText("0");
treeTable.getColumns().clear();
dc = new DataConstructor();
}

JavaFX FullScreen Mode not working

I have a basic JavaFX application, that opens as so
public class MyApplication extends Application {
private Stage stage;
public static void main(String[] args) {
Console.setDebug();
launch(args);
}
#Override
public void start(Stage primaryStage) {
// set stage as primary
stage = primaryStage;
stage.setFullscreen(true);
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we) {
System.out.println("Closing application...");
}
});
}
}
Now to change screens i have this function, which i call
private void replaceScene(String resource, IControlledScreen controller) {
try {
controller.setApp(this);
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
loader.setController(controller);
Pane screen = (Pane) loader.load();
Scene scene = new Scene(screen);
stage.setScene(scene);
stage.setFullscreen(true);
} catch (Exception e) {
System.out.println("Cannot load resource " + resource);
System.out.println(e.getMessage());
}
}
Example call would be
public void goToHome() {
replaceScene("/fxml/HomeView.fxml", new HomeController());
}
Now when i run the application, the first screen is in fullscreen mode, then when i change screens, the screen resizes to window size, then changes again to full screen??? I have tried adding
stage.setFullscreen(false);
before i call
stage.setFullscreen(true);
but this does work either. How can i change screens/scenes without it resizing?
Also is is possible to toggle full screen mode using code, say if i want the user to be able to select full screen mode, can this be done?
This is the solution:
private void replaceScene(String resource, IControlledScreen controller) {
try {
controller.setApp(this);
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
loader.setController(controller);
Pane screen = (Pane) loader.load();
stage.getScene().setRoot(screen);
} catch (Exception e) {
System.out.println("Cannot load resource " + resource);
System.out.println(e.getMessage());
}
}
Thanks to Mailkov for his code, nearly worked, but had to change it slightly, to cope with the first screen loading. This now works using
private void replaceScene(String resource, IControlledScreen controller) {
try {
controller.setApp(this);
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
loader.setController(controller);
Pane screen = (Pane) loader.load();
if (stage.getScene() == null) {
Scene scene = new Scene(screen);
stage.setScene(scene);
} else {
stage.getScene().setRoot(screen);
}
} catch (Exception e) {
System.out.println("Cannot load resource " + resource);
System.out.println(e.getMessage());
}
}

Categories

Resources