Binding JavaFX Label to StringProperty - javafx

In my JavaFX application I have a label that I want to update with the StringProperty of another class that defines server functionality.
In the server class I have a StringProperty defined as shown below:
public class eol_server {
private StringProperty serverMessagesProperty;
etc..
I have a method to return the StringProperty on request:
public StringProperty serverMessagesProperty() {
if (serverMessagesProperty == null) {
serverMessagesProperty = new SimpleStringProperty();
}
return serverMessagesProperty;
}
In the main gui class, Start() method I build the Scene, then instantiate a new server object. After this I update one of the labels in my Scene graph by binding it to the StringProperty of the server object:
public void start(Stage primaryStage) {
...set up all the gui components
primaryStage.show();
dss = new eol_server();
lbl_dssMessage.textProperty().bind(dss.getServerMessagesProperty());
}
When I run the application, the scene is rendered as I expect, and the lbl_dssMessage text is set to value that is set up in the constructor of eol_server. But, from that point on the binding is not working, although I have actions that would update the StringProperty of the dss object they are not updating the label in the GUI.
Here is the complete file that generates a stripped down version of the scene:
public class eol_gui extends Application {
private static eol_server dss = null;
private static Stage primaryStage;
/**
* Application Main Function
* #param args
*/
public static void main(String[] args) {
launch(args);
}
/**
* #param stage
*/
private void setPrimaryStage(Stage stage) {
eol_gui.primaryStage = stage;
}
/**
* #return Stage for GUI
*/
static public Stage getPrimaryStage() {
return eol_gui.primaryStage;
}
/* (non-Javadoc)
* #see javafx.application.Application#start(javafx.stage.Stage)
*/
#Override
public void start(Stage primaryStage) {
setPrimaryStage(primaryStage);
BorderPane root = new BorderPane();
Label lbl_dssMessage = new Label("initialized");
HBox topMenu = new HBox();
topMenu.getChildren().add(lbl_dssMessage);
eol_supervisor_I config1 = new eol_supervisor_I();
// Build Scene
root.setStyle("-fx-background-color: #FFFFFF;");
root.setTop(topMenu);
primaryStage.setScene(new Scene(root, 300, 50));
primaryStage.show();
dss = new eol_server();
dss.getServerMessagesProperty().addListener(new ChangeListener<Object>() {
#Override
public void changed(ObservableValue<?> o,Object oldVal,Object newVal)
{
//Option 1: lbl_dssMessage.setText(newVal.toString());
}
});
//Option 2
lbl_dssMessage.textProperty().bind(dss.serverMessagesProperty);
}
}
As you can see I have tried the bind method and a change listener. It looks like bind was working, as was the listener, in all cases except those that run in the server service threads. These throw the IllegalStateException due to not being on the main JavaFX application thread. How do I safely and correctly exchange messages from the service to the main thread?
The server is defined in the following class, which is intended to run services independent of the main JavaFX thread. But I would like to exchange info between the threads to show status. Ii'm trying to avoid the GUI hanging while the server connections and data exchanges are made.
public class eol_server {
public StringProperty serverMessagesProperty;
public eol_server() {
/* Create Scripting Environment */
serverMessagesProperty().set("Establishing Debug Server Environment");
connect();
}
public boolean connect() {
serverMessagesProperty().set("Creating Server");
ConnectService connectService = new ConnectService();
connectService.start();
return false;
}
public StringProperty serverMessagesProperty() {
if (serverMessagesProperty == null) {
serverMessagesProperty = new SimpleStringProperty();
}
return serverMessagesProperty;
}
public StringProperty getServerMessagesProperty() {
return serverMessagesProperty;
}
private class ConnectService extends Service<Void> {
#Override
protected void succeeded() {
}
#Override
protected void failed() {
}
#Override
protected void cancelled() {
}
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
serverMessagesProperty().set("Connecting....");
Thread.sleep(5000);
// DEMO: uncomment to provoke "Not on FX application thread"-Exception:
// connectButton.setVisible(false);
serverMessagesProperty().set("Waiting for server feedback");
Thread.sleep(5000);
return null;
}
};
}
}
private class DisconnectService extends Service<Void> {
#Override
protected void succeeded() {
}
#Override
protected void cancelled() {
}
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
updateMessage("Disconnecting....");
serverMessagesProperty().set("Disconnecting....");
Thread.sleep(5000);
updateMessage("Waiting for server feedback.");
serverMessagesProperty().set("Waiting for server feedback.");
Thread.sleep(5000);
return null;
}
};
}
}
}
Thanks in advance
JW

Read the below paragraph from JavaFX Node documentation.
Node objects may be constructed and modified on any thread as long
they are not yet attached to a Scene in a Window that is showing. An
application must attach nodes to such a Scene or modify them on the
JavaFX Application Thread.
The correct and more functional way to solve your problem is
dss.serverMessagesProperty.addListener((observable, oldValue, newValue) -> Platform.runLater(() -> lbl_dssMessage.setText(newValue));

I seem to have a solution, derived from what I read on the runlater() Platform method. Also see discussion here
Multi-Threading error when binding a StringProperty
changing my listener to invoke the message update via runLater seems to break the multi-threading problem. Yes I expect it is not immediate but it will be very very close to immediate and good enough. I appreciate that JavaFX requires you to not mess with the Application thread values / nodes etc etc of the scene graph but it is quite a complicated area of the JavaFX library.
Here is the listener that worked for me
// Get new DSS session active
dss = new eol_server();
dss.serverMessagesProperty.addListener(new ChangeListener<Object>() {
#Override
public void changed (ObservableValue<?> observable, Object oldValue, Object newValue) {
Platform.runLater(() -> lbl_dssMessage.setText(newValue.toString()));
}
});
}
Happy for further discussion as to why this works when the other options did not, also open to any other more elegant suggestions.

Related

Javafx run another class using hyperlink [duplicate]

I've been smashing my head with JavaFx...
This works for when there's no instances of an application running:
public class Runner {
public static void main(String[] args) {
anotherApp app = new anotherApp();
new Thread(app).start();
}
}
public class anotherApp extends Application implements Runnable {
#Override
public void start(Stage stage) {
}
#Override
public void run(){
launch();
}
}
But if I do new Thread(app).start() within another application I get an exception stating that I can't do two launches.
Also my method is called by an observer on the other application like this:
#Override
public void update(Observable o, Object arg) {
// new anotherApp().start(new Stage());
/* Not on FX application thread; exception */
// new Thread(new anotherApp()).start();
/* java.lang.IllegalStateException: Application launch must not be called more than once */
}
It's within a JavaFX class such as this:
public class Runner extends Applications implements Observer {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage){
//...code...//
}
//...methods..//
//...methods..//
#Override
public void update(Observable o, Object arg) {
//the code posted above//
}
}
I tried using ObjectProperties with listeners but it didn't work. I need to get this new stage running from within the update method from java.util.observer in some way.
Any suggestions are welcomed. Thanks.
Application is not just a window -- it's a Process. Thus only one Application#launch() is allowed per VM.
If you want to have a new window -- create a Stage.
If you really want to reuse anotherApp class, just wrap it in Platform.runLater()
#Override
public void update(Observable o, Object arg) {
Platform.runLater(new Runnable() {
public void run() {
new anotherApp().start(new Stage());
}
});
}
I did a constructor of another JFX class in Main class AnotherClass ac = new AnotherClass(); and then called the method ac.start(new Stage);. it worked me fine. U can put it either in main() or in another method. It does probably the same thing the launch(args) method does
Wanted to provide a second answer because of one caveat of using Application.start(Stage stage).
The start method is called after the init method has returned
If your JavaFX application has Override Application.init() then that code is never executed. Neither is any code you have in the second application main method.
Another way to start a second JavaFX application is by using the ProcessBuilder API to start a new process.
final String javaHome = System.getProperty("java.home");
final String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
final String classpath = System.getProperty("java.class.path");
final Class<TestApplication2> klass = TestApplication2.class;
final String className = klass.getCanonicalName();
final ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp", classpath, className);
final Button button = new Button("Launch");
button.setOnAction(event -> {
try {
Process process = builder.start();
} catch (IOException e) {
e.printStackTrace();
}
});

How to prepare FXML Controller to be GC

I have an issue in my application. Any Controllers are garbage collected.
I was prepared one, pretty simple with clear function, but it is no removed from memory.
#Log4j2
public class DialogOpenProjectController extends DialogPane implements Initializable, FXMLController, FXMLPane, FXMLDialogController {
#FXML
private ObjectProperty<ResourceBundle> languageBundle = new SimpleObjectProperty<>();
#FXML
private JFXTabPane TAB_PANE_OPEN_PROJECT;
#FXML
private JFXButton BUTTON_CONFIRM;
private Tab tabOpenNewProject;
private Tab tabOpenExistingProject;
private Stage stage;
private ChangeListener<? super ResourceBundle> languageListener = this::languageChange;
private ChangeListener<? super Tab> selectedTabListener = this::selectedTabChanged;
{
tabOpenExistingProject = new Tab();
tabOpenNewProject = new Tab();
}
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
log.trace(LogMessages.MSG_CTRL_INITIALIZATION);
this.languageBundle.setValue(rb);
initTabs();
addSelectedTabListener();
addDisableButtonListener();
addLanguageListener();
log.trace(LogMessages.MSG_CTRL_INITIALIZED);
}
#FXML
public void cancel() {
this.stage.close();
clean();
}
#FXML
public void confirm() {
if (TAB_PANE_OPEN_PROJECT.getSelectionModel().getSelectedItem().equals(tabOpenNewProject)) {
actionNewProject();
} else if (TAB_PANE_OPEN_PROJECT.getSelectionModel().getSelectedItem().equals(tabOpenExistingProject)) {
actionOpenProject();
} else {
//To to show error
}
this.stage.close();
clean();
}
private void initTabs() {
TAB_PANE_OPEN_PROJECT.getSelectionModel().select(tabOpenNewProject);
}
private void addSelectedTabListener() {
TAB_PANE_OPEN_PROJECT.getSelectionModel().selectedItemProperty().addListener(selectedTabListener);
}
private void addDisableButtonListener() {
//nothing to do temporary
}
private void clean() {
this.languageBundle.removeListener(languageListener);
languageBundle.unbind();
languageBundle.setValue(null);
TAB_PANE_OPEN_PROJECT.getSelectionModel().selectedItemProperty().removeListener(selectedTabListener);
TAB_PANE_OPEN_PROJECT.getSelectionModel().clearSelection();
TAB_PANE_OPEN_PROJECT.getTabs().clear();
BUTTON_CONFIRM.disableProperty().unbind();
selectedTabListener = null;
languageListener = null;
tabOpenNewProject = null;
tabOpenExistingProject = null;
stage = null;
getChildren().clear();
}
private void addLanguageListener() {
this.languageBundle.addListener(languageListener);
}
private void languageChange(ObservableValue<? extends ResourceBundle> observable, ResourceBundle oldValue, ResourceBundle newValue) {
reloadElements();
}
private String getValueFromKey(String key) {
return this.languageBundle.getValue().getString(key);
}
private void reloadElements() {
// Nothing to do
}
public void setStage(Stage stage) {
this.stage = stage;
}
private void selectedTabChanged(ObservableValue<? extends Tab> observable, Tab oldValue, Tab newValue) {
if (newValue.equals(tabOpenNewProject)) {
BUTTON_CONFIRM.setText(getValueFromKey(Keys.CREATE));
} else if (newValue.equals(tabOpenExistingProject)) {
BUTTON_CONFIRM.setText(getValueFromKey(Keys.OPEN));
}
}
}
For loading FXML files I use Singleton Class ScreenManager. This method is called to load this Dialog :
public void showNewDialog( FilesFXML fxml) {
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml.toString()), context.getBundleValue());
try {
Stage dialogStage = new Stage();
AnchorPane dialogwindow = (AnchorPane) loader.load();
FXMLDialogController controller = loader.getController();
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(this.getStage());
Scene scene = new Scene(dialogwindow);
dialogStage.setScene(scene);
dialogStage.setResizable(false);
controller.setStage(dialogStage);
dialogStage.showAndWait();
} catch (Exception ex) {
log.error(ex.getMessage());
log.error(ex.getCause());
ex.printStackTrace();
}
}
I was checked it in VisualVM and I see one 1 of this controller and 2 lambdas ( I suppose it consiste initialization of 2 listener)
But event clear function is called this dialog is still in memory and cannot be garbage collected. I have no more idea how to force removing it. It is reallegrave, because it consist all of my Controllers.
I'm pretty confused what you are asking and what you are talking about. Nevertheless, if that dialog window is open, there is no way it can be garbage collected.
The JavaFX main API is holding a strong reference of the stage (maybe the scene too).
The stage/scene is holding strong reference of all the nodes specified in your FXML file.
Some of those nodes are holding strong reference of the controller because you have specified action event handlers in the controller.
In order to make the controller eligible for garbage collection, you need to break this chain somewhere.
The easiest way is to close the dialog window. When the window is closed, because the JavaFX engine does not need to manage render pulses anymore, it will release its reference to the stage. At that point, the whole thing under that stage is eligible for garbage collection, if nothing else (that itself is not eligible for garbage collection) is holding strong references of them.

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 - Using Progressbar on dynamically loaded Gridpane

I'm using a GridPane, where I'm loading my data from a DB to the Pane, creating rows and columns dynamically. It works fine, but sometimes, if there is a lot of stuff (about 30 rows and 30 columns, and every cell has a EventListener) to load, it takes more than a second to see the loaded stuff.
So, I thought, that it will be great to add a ProgressBar or a ProgressIndicator. I tried that, but I realized that it does not depend on the loops, which are filling and creating the dynamic GridPane, but on the fact that the "loading and applying" takes a lot of time. My ProgressIndicator jumps from 0 to 100 and that's not what I want.
Do you have some ideas how to solve that problem? I read some articles about Preloaders, and I'm looking for a similar functionality, but I can't use Preloaders for that problem.
Best regards,
Edit: Now I got a solution, which works, but I don't think that someone else would write such code :S.
I created a class called LoaderDienstplan:
public class LoaderDienstplan extends Task {
//Some member variables for starting call() method
#Override
protected Object[] call() throws Exception{
// calling DB and service
updateProgress(1,1);
return; // Returning an ObjectArray with Lists,Data and a reference to my Controllerclass
}
#Override
public void run(){
final Object[] b = call();
Platform.runLater(new Runnable(){
#Override
public void run(){
//Updating GridPane
}
});
}
}
public class LoaderDienstplan extends Task {
MyInterface listener;
//Some member variables for starting call() method
#Override
protected Object[] call() throws Exception{
// calling DB and service
updateProgress(1,1);
return; // Returning an ObjectArray with Lists,Data and a reference to my Controllerclass
}
public void setListener(MyInterface listener) {
this.listener = listener;
}
#Override
public void run() {
final Object[] b = call();
Platform.runLater(new Runnable(){
#Override
public void run() {
//Updating GridPane
listener.updateGUI(something);
}
});
}
}
And the interface:
public interface MyInterface {
void updateGUI(Something something);
void loadingFinished();
}
And from your Controller class, must implements MyInterface:
LoaderDienstplan load = new LoaderDienstplan(...);
load.setListener(this);
Make this class:
public class Loader implements Runnable {
private final MyController listener;
private final List<File> stuff;
public DoubleProperty progressProperty;
public Loader(MyController listener, List<File> stuffToLoad) {
this.stuff = stuffToLoad;
progressProperty = new SimpleDoubleProperty();
this.listener = listener;
}
private void updateProgress(double value, double max) {
progressProperty.set(value / max);
}
public DoubleProperty progressProperty() {
return progressProperty;
}
#Override
public void run() {
Platform.runLater(new Runnable() {
public void run() {
//load the stuffs
}
});
//each time you load another stuff, do:
updateProgress(index, totalAmountOfStuffs);
//when finished, do:
listener.finished();
}
}
Put a veil and an indicator around the GridPane, like this (i made the veil (Region) and indicator (ProgressIndicator) from the JavaFX SceneBuilder):
//lets say your progress indicator is called progressIndicator and the shaded `Region` is called veil
progressIndicator.setMaxSize(150, 150);
veil.setStyle("-fx-background-color: rgba(0, 0, 0, 0.4)");
And run the Loader class from another thread like this:
private void startLoading() {
progressIndicator.setVisible(true);
veil.setVisible(true);
Loader loader = new Loader(this, stuffToLoad);
progressIndicator.progressProperty().bind(loader.progressProperty());
new Thread(loader).start();
}
#Override
public void finished() {
progressIndicator.setVisible(false);
veil.setVisible(false);
}
What it looks like for me:

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