how javafx stage creation is done - javafx-2

I am just a beginner in javafx.I was just seeing some examples in ensemble.jar and has a doubt in the following program.Here in this there are 2 methods start and init both of which accepts arguments of type Stage. init() is called from start().My doubt is stage decoration(adding group,progressindicator,gridpane) is done in start method.So primaryStage.show() will display the decorated stage but here if I write primaryStage1.show() in the start() then also decorated stage is also displaying.I want to know how
package fx;
/**
* Copyright (c) 2008, 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.GridPane;
/**
* A sample that demonstrates the Progress Indicator control in various modes.
*
* #see javafx.scene.control.ProgressIndicator
* #related controls/ProgressBar
*/
public class ProgressIndicatorSample extends Application {
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 400,400));
GridPane g = new GridPane();
ProgressIndicator p1 = new ProgressIndicator();
p1.setPrefSize(50, 50);
ProgressIndicator p2 = new ProgressIndicator();
p2.setPrefSize(50, 50);
p2.setProgress(0.25F);
ProgressIndicator p3 = new ProgressIndicator();
p3.setPrefSize(50, 50);
p3.setProgress(0.5F);
ProgressIndicator p4 = new ProgressIndicator();
p4.setPrefSize(50, 50);
p4.setProgress(1.0F);
g.add(p1, 1, 0);
g.add(p2, 0, 1);
g.add(p3, 1, 1);
g.add(p4, 2, 1);
char x[]={'a','m'};
x.toString();
System.out.println(x);
g.setHgap(40);
g.setVgap(40);
root.getChildren().add(g);
}
public double getSampleWidth() { return 400; }
public double getSampleHeight() { return 400; }
#Override public void start(Stage primaryStage1) throws Exception {
init(primaryStage1);
primaryStage1.show();
}
public static void main(String[] args) { launch(args); }
}

On application stage creation
On launching an application, the JavaFX system creates an initial stage for the system and, without showing it, passes a reference to this stage object to the application's start method.
Explaining what happens in your sample program
There is only one stage involved here.
When you launch a JavaFX application (as you do in your main method), the JavaFX toolkit system is started. The JavaFX system creates an initial stage (window) for your application, but does not show it, instead it passes a reference to that stage to your start method (which you accept as the parameter primaryStage1). You than pass that stage reference to the init method, which places all of the content (groups, progress bars and so forth) into a scene which it places on the stage. Finally, execution returns to your start method and you request that the stage be shown (which tells the JavaFX system to display the stage on the screen and render the content inside the stage).
In addition to the initial stage created by the JavaFX system and passed into your program, you could have created more new stages in your program so that you have additional stages (windows) for pop-up dialog boxes or other reasons, but you did not.
On creating additional stages
To create more new stages, you can do something like the following (from Sergey's answer to How to create and show common dialog (Error, Warning, Confirmation) in JavaFX 2.0?)
Stage dialogStage = new Stage();
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create().
children(new Text("Hi"), new Button("Ok.")).
alignment(Pos.CENTER).padding(new Insets(5)).build()));
dialogStage.show();
On object reference passing
See also:
Is Java "pass-by-reference" or "pass-by-value"? to understand how the stage is being passed around in your program.

Related

Is it possible to show a Node during FX thread

I'm making a program which reads text files. What I would like to do is show an arbitrary node (Alert or other Node) which is created in separate thread before or during the file reading. I tried using Task and Platform.runLater() like this:
if (filetoopen != null)
{
Platform.runLater(new Runnable() {
#Override
void run() {
Alert alert=new Alert(Alert.AlertType.INFORMATION)
alert.setHeaderText('TEST')
}
})
//method to read the file
Tools.convertFromFile(filetoopen,newredactor)
lastDirectory = filetoopen.getParentFile()
}
I'd like to show an Alert or progress bar of reading the file, but the Control initializes after the reading is finished. So, is it possible to show a Node with a progress bar while the file is being read? Or the Runnable I create will always be executed in the end?
Edit: an attempt with Task:
class Alerter extends Task{
Alerter(File f,Editor e)
{
file=f
editor=e
}
File file
Editor editor
#Override
protected Object call() throws Exception {
Dialog dialog=new Dialog()
DialogPane dp=dialog.getDialogPane()
dp.setHeaderText('TEST')
dp.getButtonTypes().add(new ButtonType('Cancel',ButtonBar.ButtonData.CANCEL_CLOSE))
dialog.setOnCloseRequest(new javafx.event.EventHandler<DialogEvent>() {
#Override
void handle(DialogEvent event) {
dialog.close()
}
})
dialog.show()
Tools.convertFromFile(file,editor)
return null
}
}
The dialog still initializes after Tools.convertFromFile.
There are two threading rules in JavaFX (and in almost every other UI toolkit):
Changes to the scene graph (i.e. creating new scenes or windows, or changing the state of nodes already displayed) must be done on the FX Application Thread.
Long-running processes should be performed on a background thread (i.e. not the FX Application Thread), otherwise the UI will become unresponsive.
Your first code block violates the second rule (probably, you haven't shown much context) and your second code block violates the first rule.
So basically you need to:
Show the dialog from the FX Application Thread
Start a new thread which processes the file in the background
From the new thread, schedule any changes to the new UI on the FX Application Thread
When processing the file finishes, update the UI on the FX Application Thread
You can use Platform.runLater(...) to schedule code to run on the FX Application Thread, but the Task class provides more convenient API for these updates.
So:
// set up and show dialog:
ProgressBar progressBar = new ProgressBar();
DialogPane dialogPane = new DialogPane();
dialogPane.getButtonTypes().setAll(ButtonType.OK);
dialogPane.setHeaderText("Processing file");
dialogPane.setContent(progressBar);
dialogPane.lookupButton(ButtonType.OK).setDisable(true);
Dialog dialog = new Dialog();
dialog.setDialogPane(dialogPane);
dialog.show();
// create task:
Task<Void> task = new Task<Void>() {
#Override
public Void call() throws Exception {
Tools.convertFromFile(file, editor);
// can call updateProgress(...) here to update the progress periodically
return null ;
}
};
// update progress bar with progress from task:
progressBar.progressProperty().bind(task.progressProperty());
// when task completes, update dialog:
task.setOnSucceeded(event -> {
dialogPane.lookupButton(ButtonType.OK).setDisable(false);
progressBar.progressProperty().unbind();
progressBar.setProgress(1);
dialogPane.setHeaderText("Processing complete");
});
// handles errors:
task.setOnFailed(event -> {
dialogPane.lookupButton(ButtonType.OK).setDisable(false);
progressBar.progressProperty().unbind();
progressBar.setProgress(0);
dialogPane.setHeaderText("An error occurred");
});
// run task in background thread:
Thread thread = new Thread(task);
thread.start();
Note here that your Tools.convertFromFile(...) method is called from a background thread, so it must not update the UI (or at least any calls in that method that do update the UI must be wrapped in Platform.runLater(...)).
Here is a complete SSCCE (which just sleeps as a demo of a long-running process):
import java.util.Random;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TaskWithProgressDemo extends Application {
#Override
public void start(Stage primaryStage) {
Button button = new Button("Start process");
button.setOnAction(e -> {
button.setDisable(true);
// set up and show dialog:
ProgressBar progressBar = new ProgressBar();
DialogPane dialogPane = new DialogPane();
dialogPane.getButtonTypes().setAll(ButtonType.OK);
dialogPane.setHeaderText("Processing file in progress");
dialogPane.setContent(progressBar);
dialogPane.lookupButton(ButtonType.OK).setDisable(true);
Dialog<Void> dialog = new Dialog<Void>();
dialog.setDialogPane(dialogPane);
dialog.show();
// create task:
Task<Void> task = new Task<Void>() {
#Override
public Void call() throws Exception {
Random rng = new Random();
for (int i = 0 ; i <= 100 ; i++) {
Thread.sleep(rng.nextInt(40));
updateProgress(i, 100);
}
if (rng.nextBoolean()) {
System.out.println("Simulated error");
throw new Exception("An unknown error occurred");
}
return null ;
}
};
// update progress bar with progress from task:
progressBar.progressProperty().bind(task.progressProperty());
// when task completes, update dialog:
task.setOnSucceeded(event -> {
dialogPane.lookupButton(ButtonType.OK).setDisable(false);
button.setDisable(false);
progressBar.progressProperty().unbind();
progressBar.setProgress(1);
dialogPane.setHeaderText("Processing complete");
});
// handles errors:
task.setOnFailed(event -> {
dialogPane.lookupButton(ButtonType.OK).setDisable(false);
button.setDisable(false);
progressBar.progressProperty().unbind();
progressBar.setProgress(0);
dialogPane.setHeaderText("An error occurred");
});
// run task in background thread:
Thread thread = new Thread(task);
thread.start();
});
StackPane root = new StackPane(button);
root.setPadding(new Insets(20));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
So I finally figured it out. I had to move both my file loading code and progress update to a Task, so it wouldn't block FX thread. The indicator shows progress of loading a file.
Edit: to achieve progress display in a separate non-blocking window, must use a new Stage instead of anything else.

JavaFX "already set as root of another scene" exception when launching modal dialog a second time

I am building a simple JavaFX application that requires me to launch a modal window in front of my main application window. Using the code below, I am able to launch the modal window 1 time and close it. If I attempt to launch it again, I receive:
java.lang.IllegalArgumentException: BorderPane[id=root, styleClass=root]is already set as root of another scene
I am using the Spring Controller/FXML View dependency injection method described here:
http://www.zenjava.com/2012/02/20/porting-first-contact-to-spring/
I am able to programatically create a scene and hide/display a simple dialog without using FXML / Spring Controller injection. This works fine.
I am unable to explain the 'already set as root' exception, as I am creating a new Scene() each time the startButton is clicked. The 1st Scene should have been destroyed whenever the modal window was closed the 1st time.
Relevant files are below.
MainTabPanel.java - The main view of my application. This contains the 'startButton' that is clicked to launch the modal window.
The ActivePresentation Controller/View is injected as:
#Inject private ActivePresentation activePresentation;
Below is the initialize() method that attempts to launch the modal when startButton is clicked.
#FXML
public void initialize()
{
availableReceiversIdColumn.setCellValueFactory(new PropertyValueFactory("id"));
availableReceiversFirmwareVersionColumn.setCellValueFactory(new PropertyValueFactory("firmwareVersion"));
availableReceiversModelColumn.setCellValueFactory(new PropertyValueFactory("model"));
availableReceiversChannelColumn.setCellValueFactory(new PropertyValueFactory("channel"));
ObservableList<String> responseTypes = FXCollections.observableArrayList();
responseTypes.add("Single Response Alpha");
responseTypes.add("Single Response Numeric");
responseTypeChoiceBox.setItems(responseTypes);
startButton.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
presentationResponseService.startPresentation();
activePresentation.populateResponses(null);
activePresentation.populateResults(null);
Scene activePresentationScene = new Scene(activePresentation.getView());
activePresentationScene.getStylesheets().add("styles.css");
stage.setScene(activePresentationScene);
stage.setTitle("Active Presentation");
stage.showAndWait();
}
});
}
The closeButton is defined in the modal dialog as follows.
closeButton.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
presentationResponseService.closePresentation();
Stage stage = (Stage) root.getScene().getWindow();
stage.close();
}
});
The Java based Spring configuration for the ActivePresentation bean and FXML loader are as follows.
#Bean
public ActivePresentation activePresentation()
{
return loadPresenter("/fxml/ActivePresentation.fxml");
}
FXML Loader
private <T> T loadPresenter(String fxmlFile)
{
try
{
FXMLLoader loader = new FXMLLoader();
loader.load(getClass().getResourceAsStream(fxmlFile));
return (T) loader.getController();
} catch (IOException e)
{
throw new RuntimeException(String.format("Unable to load FXML file '%s'", fxmlFile), e);
}
}
I had the issue when I had a pop-up window autowired. it was working the first time but when I close the pop-up window the second time, I was getting this error.
I managed by checking if the pop-up window had already a scene, see the below code:
if (window.getScene() == null) {
Scene scene = new Scene(window);
stage.setScene(scene);
} else {
stage.setScene(window.getScene());
}
Seems, the stage is still under control of JFX (logic : you can try yo open it again).
And you create a stage each time while previous is still alive, so, could you try to add stage.setScene(null) near stage.close().
Or use the same scene or the same stage each time?
I don't see any bug in this part of JFX : node is the same, stages (and scenes in it) are different. So there are 2 ways : use only 1 stage+scene, or create different border pane instances each time.

JavaFX: How to bind two values?

I'm new guy here :)
I have a small problem which concerns binding in JavaFX. I have created Task which is working as a clock and returns value which has to be set in a special label (label_Time). This label presents how many seconds left for player's answer in quiz.
The problem is how to automatically change value in label using the timer task? I tried to link value from timer Task (seconds) to label_Time value in such a way...
label_Time.textProperty().bind(timer.getSeconds());
...but it doesn't work. Is it any way to do this thing?
Thanks in advance for your answer! :)
Initialize method in Controller class:
public void initialize(URL url, ResourceBundle rb) {
Timer2 timer = new Timer2();
label_Time.textProperty().bind(timer.getSeconds());
new Thread(timer).start();
}
Task class "Timer2":
public class Timer2 extends Task{
private static final int SLEEP_TIME = 1000;
private static int sec;
private StringProperty seconds;
public Timer2(){
Timer2.sec = 180;
this.seconds = new SimpleStringProperty("180");
}
#Override protected StringProperty call() throws Exception {
int iterations;
for (iterations = 0; iterations < 1000; iterations++) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
System.out.println("TIK! " + sec);
seconds.setValue(String.valueOf(sec));
System.out.println("TAK! " + seconds.getValue());
// From the counter we subtract one second
sec--;
//Block the thread for a short time, but be sure
//to check the InterruptedException for cancellation
try {
Thread.sleep(10);
} catch (InterruptedException interrupted) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
}
}
return seconds;
}
public StringProperty getSeconds(){
return this.seconds;
}
}
Why your app does not work
What is happening is that you run the task on it's own thread, set the seconds property in the task, then the binding triggers an immediate update of the label text while still on the task thread.
This violates a rule for JavaFX thread processing:
An application must attach nodes to a Scene, and modify nodes that are already attached to a Scene, on the JavaFX Application Thread.
This is the reason that your originally posted program does not work.
How to fix it
To modify your original program so that it will work, wrap the modification of the property in the task inside a Platform.runLater construct:
Platform.runLater(new Runnable() {
#Override public void run() {
System.out.println("TIK! " + sec);
seconds.setValue(String.valueOf(sec));
System.out.println("TAK! " + seconds.getValue());
}
});
This ensures that when you write out to the property, you are already on the JavaFX application thread, so that when the subsequent change fires for the bound label text, that change will also occur on the JavaFX application thread.
On Property Naming Conventions
It is true that the program does not correspond to JavaFX bean conventions as Matthew points out. Conforming to those conventions is both useful in making the program more readily understandable and also for making use of things like the PropertyValueFactory which reflect on property method names to allow table and list cells to automatically update their values as the underlying property is updated. However, for your example, not following JavaFX bean conventions does not explain why the program does not work.
Alternate Solution
Here is an alternate solution to your countdown binding problem which uses the JavaFX animation framework rather than the concurrency framework. I prefer this because it keeps everything on the JavaFX application thread and you don't need to worry about concurrency issues which are difficult to understand and debug.
import javafx.animation.*;
import javafx.application.Application;
import javafx.beans.*;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class CountdownTimer extends Application {
#Override public void start(final Stage stage) throws Exception {
final CountDown countdown = new CountDown(10);
final CountDownLabel countdownLabel = new CountDownLabel(countdown);
final Button countdownButton = new Button(" Start ");
countdownButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent t) {
countdownButton.setText("Restart");
countdown.start();
}
});
VBox layout = new VBox(10);
layout.getChildren().addAll(countdownLabel, countdownButton);
layout.setAlignment(Pos.BASELINE_RIGHT);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 20; -fx-font-size: 20;");
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
class CountDownLabel extends Label {
public CountDownLabel(final CountDown countdown) {
textProperty().bind(Bindings.format("%3d", countdown.timeLeftProperty()));
}
}
class CountDown {
private final ReadOnlyIntegerWrapper timeLeft;
private final ReadOnlyDoubleWrapper timeLeftDouble;
private final Timeline timeline;
public ReadOnlyIntegerProperty timeLeftProperty() {
return timeLeft.getReadOnlyProperty();
}
public CountDown(final int time) {
timeLeft = new ReadOnlyIntegerWrapper(time);
timeLeftDouble = new ReadOnlyDoubleWrapper(time);
timeline = new Timeline(
new KeyFrame(
Duration.ZERO,
new KeyValue(timeLeftDouble, time)
),
new KeyFrame(
Duration.seconds(time),
new KeyValue(timeLeftDouble, 0)
)
);
timeLeftDouble.addListener(new InvalidationListener() {
#Override public void invalidated(Observable o) {
timeLeft.set((int) Math.ceil(timeLeftDouble.get()));
}
});
}
public void start() {
timeline.playFromStart();
}
}
Update for additional questions on Task execution strategy
Is it possible to run more than one Task which includes a Platform.runLater(new Runnable()) method ?
Yes, you can use multiple tasks. Each task can be of the same type or a different type.
You can create a single thread and run each task on the thread sequentially, or you can create multiple threads and run the tasks in parallel.
For managing multiple tasks, you can create an overseer Task. Sometimes it is appropriate to use a Service for managing the multiple tasks and the Executors framework for managing multiple threads.
There is an example of a Task, Service, Executors co-ordination approach: Creating multiple parallel tasks by a single service In each task.
In each task you can place no runlater call, a single runlater call or multiple runlater calls.
So there is a great deal of flexibility available.
Or maybe I should create one general task which will be only take data from other Tasks and updating a UI?
Yes you can use a co-ordinating task approach like this if complexity warrants it. There is an example of such an approach in in Render 300 charts off screen and save them to files.
Your "Timer2" class doesn't conform to the JavaFX bean conventions:
public String getSeconds();
public void setSeconds(String seconds);
public StringProperty secondsProperty();

Create new Tooltip on not JavaFX Application Thread

is there a way how to create javafx.scene.control.Tooltip in thread which is not "JavaFX Application Thread"?
Because of performance I prepare some visual elements in one thread and keep them in memory. And when I need them I use Application Thread for showing them. But new Tooltip() seems to require Application Thread only.
is there a way how to create javafx.scene.control.Tooltip in thread which is not "JavaFX Application Thread"?
For JavaFX 2.2 - No.
See this JavaFX issue tracker tiecket: Tooltip of Tab (and other controls) cannot be set of FXApplicationThread.
There is a suggested workaround in the ticket to create the Tooltip in a Platform.runLater call.
package fxthread_25127_wrongthread;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;
/**
*
* #author mickf
*/
public class Main extends Application {
Tooltip tooltip;
public static void main(String[] args) {
Application.launch(args);
}
#Override public void init() throws Exception {
Platform.runLater(new Runnable() {
#Override public void run() {
tooltip = new Tooltip("Top tip : use runLater()");
}
});
/*
* this will cause an ISE
*/
//tooltip = new Tooltip("flip");
}
#Override public void start(Stage s) {
System.exit(0);
}
}
A description of how Platform.runLater works from it's javadoc:
Run the specified Runnable on the JavaFX Application Thread at some unspecified time in the future. This method, which may be called from any thread, will post the Runnable to an event queue and then return immediately to the caller. The Runnables are executed in the order they are posted. A runnable passed into the runLater method will be executed before any Runnable passed into a subsequent call to runLater.

How to remove JavaFX stage buttons (minimize, maximize, close)

How to remove JavaFX stage buttons (minimize, maximize, close)? Can't find any according Stage methods, so should I use style for the stage? It's necessary for implementing Dialog windows like Error, Warning, Info.
If you want to disable only the maximize button then use :
stage.resizableProperty().setValue(Boolean.FALSE);
or if u want to disable maximize and minimize except close use
stage.initStyle(StageStyle.UTILITY);
or if you want to remove all three then use
stage.initStyle(StageStyle.UNDECORATED);
You just have to set a stage's style. Try this example:
package undecorated;
import javafx.application.Application;
import javafx.stage.StageStyle;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class UndecoratedApp extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.initStyle(StageStyle.UNDECORATED);
Group root = new Group();
Scene scene = new Scene(root, 100, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
}
When learning JavaFX 2.0 these examples are very helpful.
primaryStage.setResizable(false);
primaryStage.initStyle(StageStyle.UTILITY);
I´m having the same issue, seems like an undecorated but draggable/titled window (for aesthetic sake) is not possible in javafx at this moment. The closest approach is to consume the close event.
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
event.consume();
}
});
If you like lambdas
stage.setOnCloseRequest(e->e.consume());
stage.initModality(Modality.APPLICATION_MODAL);
stage.setResizable(false);
I found this answer here -->
http://javafxportal.blogspot.ie/2012/03/to-remove-javafx-stage-buttons-minimize.html
We can do it:
enter code here
#Override
public void start(Stage primaryStage) {
primaryStage.initStyle(StageStyle.UNDECORATED);
Group root = new Group();
Scene scene = new Scene(root, 100, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
stage.initStyle(StageStyle.DECORATED);
stage.setResizable(false);
You can achieve this, you call the following methods on your stage object
stage.initModality(Modality.APPLICATION_MODAL); // makes stage act as a modal
stage.setMinWidth(250); // sets stage width
stage.setMinHeight(250); // sets stage height
stage.setResizable(false); // prevents resize and removes minimize and maximize buttons
stage.showAndWait(); // blocks execution until the stage is closed

Resources