Timeline pause in a process - javafx

I wanna add dots to label every 0,5 seconds, but when there are three dots pause the process for 3 second, then remove the dots and start it again (3 times). I tried using this way, but it just add, not pause and not remove :
Label calling = new Label("Calling");
Timeline timer = new Timeline();
KeyFrame first = new KeyFrame(
Duration.millis(500),
a -> {
calling.setText(calling.getText()+".");
if(calling.getText().endsWith("...")){
new Timeline(new KeyFrame(Duration.seconds(3), b-> calling.setText("Calling"))).play();
}
}
);
timer.getKeyFrames().addAll(first);
timer.setCycleCount(9);
timer.play();
});

You can do
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class AddDotsToLabel extends Application {
#Override
public void start(Stage primaryStage) {
Label label = new Label("Calling");
EventHandler<ActionEvent> eventHandler = e -> label.setText(label.getText()+".");
Timeline timeline = new Timeline(
new KeyFrame(Duration.millis(500), eventHandler),
new KeyFrame(Duration.millis(1000), eventHandler),
new KeyFrame(Duration.millis(1500), eventHandler),
new KeyFrame(Duration.millis(2000), e -> label.setText("Calling")),
new KeyFrame(Duration.millis(5000))
);
timeline.setCycleCount(3);
timeline.play();
label.setPadding(new Insets(20));
primaryStage.setScene(new Scene(new HBox(label), 120, 40));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Related

JavaFX - making Timeline object on multiple objects execute EventHandler once

I am working in JavaFX right now, and have run into a problem. I created a simple example to demonstrate the issue I am facing.
My issue is, I have a Timeline object setup on multiple anonymous Circle objects and I want an action to happen after the Timeline has finished its play() method. To do this, I setup a setOnFinished event handler to execute something after the animation has finished playing, However, it is executing this logic multiple times because it is working on multiple objects.
Here I have a simple program that adds 3 anonymous objects to a VBox and there is a button that will call the flash() method to start a Timeline animation on the circles.
package sample;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
VBox root = new VBox();
VBox circles = new VBox();
Button btn = new Button("Touch me to make me flash ;)");
btn.setOnAction(e -> flash(circles));
for(int i = 0; i < 3; i++) {
circles.getChildren().add(new Circle(25, Color.RED));
}
circles.setSpacing(10);
circles.setAlignment(Pos.CENTER);
root.getChildren().addAll(circles, btn);
root.setSpacing(10);
root.setAlignment(Pos.CENTER);
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.show();
}
private void flash(VBox root) {
for(Node circle : root.getChildren()) {
final Circle c = (Circle) circle;
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(0.5), e -> c.setFill(Color.GOLD)),
new KeyFrame(Duration.seconds(1.0), e -> c.setFill(Color.RED))
);
timeline.setCycleCount(5);
timeline.play();
timeline.setOnFinished(e -> System.out.println("Do something here"));
}
}
}
You can see in the flash() method that there is an EventHandler that executes this line of code:
System.out.println("Do something here")
I want it to only be executing one time, but it executes 3 times because the Timeline object is setup on 3 circles.
How can I make it so the EventHandler only executes one time?
Consider using one TimeLine object for all Circle objects:
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
private ObjectProperty<Paint> colorProperty;
#Override
public void start(Stage primaryStage) throws Exception{
colorProperty = new SimpleObjectProperty<>(Color.WHITE);
VBox root = new VBox();
VBox circles = new VBox();
Button btn = new Button("Touch me to make me flash ;)");
btn.setOnAction(e -> flash(circles));
for(int i = 0; i < 3; i++) {
circles.getChildren().add(new Circle(25, Color.RED));
}
circles.setSpacing(10);
circles.setAlignment(Pos.CENTER);
root.getChildren().addAll(circles, btn);
root.setSpacing(10);
root.setAlignment(Pos.CENTER);
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.show();
}
private void flash(VBox root) {
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(0.5), e -> colorProperty.set(Color.GOLD)),
new KeyFrame(Duration.seconds(1.0), e -> colorProperty.set(Color.RED))
);
timeline.setCycleCount(5);
timeline.play();
timeline.setOnFinished(e -> System.out.println("Do something here"));
for(Node circle : root.getChildren()) {
final Circle c = (Circle) circle;
c.fillProperty().bind(colorProperty);
}
}
public static void main(String[] args) {
launch(null);
}
}

JavaFX : How to manage the z-index of stages

Is there any way to manage the z-index ordering of multiple stages (independent to each other). Something like, say there are three Stages A, B & C. StageA should stay always at back. StageB should be in middle and StageC should be always on top. Just a special note that these three stages have no relation to each other (like owner)
Below is a quick demo of what I am expecting. I need to access any stage (for dragging or modifying) but z-order need to be maintained. Any ideas or help is highly appreciated.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.HashMap;
import java.util.Map;
public class StagesZOrdering_Demo extends Application {
private Map<String, Stage> stages = new HashMap<>();
#Override
public void start(Stage stage) throws Exception {
Button button1 = new Button("Back");
button1.setOnAction(e -> openStage("Back"));
Button button2 = new Button("Middle");
button2.setOnAction(e -> openStage("Middle"));
Button button3 = new Button("Front");
button3.setOnAction(e -> openStage("Front"));
VBox root = new VBox(button1, button2, button3);
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
Scene sc = new Scene(root, 200, 200);
stage.setScene(sc);
stage.show();
}
private void openStage(String title) {
if (stages.get(title) != null) {
stages.get(title).requestFocus();
} else {
Stage stg = new Stage();
stg.setTitle(title);
stg.setScene(new Scene(new StackPane(), 300, 300, Color.GRAY));
stg.show();
stg.setOnHidden(e -> stages.remove(title));
stages.put(title, stg);
}
}
public static void main(String... a) {
Application.launch(a);
}
}
The following mcve demonstrates re-ordering of back-to-front stages, once a ROOT MOUSE_EXITED_TARGET event is fired from one of them.
It is a simple yet limited solution:
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventType;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class StagesZOrdering_Demo extends Application {
public enum STAGES {BACK, MIDDLE, FRONT;}
private final EnumMap<STAGES, Stage> stages = new EnumMap<>(STAGES.class);
#Override
public void start(Stage stage) throws Exception {
VBox root = new VBox();
for(STAGES s : STAGES.values()){
Button button = new Button(s.name());
button.setOnAction(e -> openStage(s));
root.getChildren().add(button);
}
root.setAlignment(Pos.CENTER);
root.setSpacing(10);
Scene sc = new Scene(root, 200, 200);
stage.setScene(sc);
stage.show();
}
private void openStage(STAGES s) {
if (stages.get(s) == null) {
Stage stg = new Stage();
stg.setTitle(s.name());
stg.addEventHandler(EventType.ROOT, e->reOrder(e));
stg.setScene(new Scene(new StackPane(), 300, 100, Color.GRAY));
stg.show();
stg.setOnHidden(e -> stages.remove(s));
stages.put(s, stg);
}
}
private void reOrder(Event e){
if(! e.getEventType().getName().equals("MOUSE_EXITED_TARGET"))
return;
for(STAGES s : STAGES.values()){
Stage stage = stages.get(s);
if(stage != null) {
stage.requestFocus();
}
}
}
public static void main(String... a) {
Application.launch(a);
}
}

Javafx label not updating after input from TextField in another scene

I have a label called test that is supposed to display the input value from TextField in the textInput class, from another scene. the value is being sent over to the main class but the label is not updating unless i click the button to go to the dialog box from textInput.
package javafx11;
import application.textInput;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
public class Main extends Application {
Stage window = new Stage();
Scene s1;
Scene s2;
Label test;
String input;
Button btn;
#Override
public void start(Stage primaryStage)throws Exception {
window=primaryStage;
VBox layout = new VBox();
s1= new Scene(layout,500,500);
test = new Label("This is where your text will appear");
btn = new Button("Click me");
window.setTitle("Dummy program");
layout.setAlignment(Pos.CENTER);
window.setScene(s1);
window.show();
btn.setOnAction(e -> {
input = textInput.textInput("title", "mnessage");
test.setText(input);
window.setScene(s1);
System.out.println(input);
});
layout.getChildren().addAll(test, btn);
}
public static void main(String[] args) {
launch(args);
}
}
the text input class:
package application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class textInput {
static String input;
public static TextField userField;
public static String textInput(String title, String message) {
Stage window = new Stage();
VBox layout = new VBox();
window.setTitle(title);
window.initModality(Modality.APPLICATION_MODAL);
Button btn = new Button("Click me to go back");
userField = new TextField();
Scene s1 = new Scene(layout, 500, 500);
window.setScene(s1);
layout.getChildren().add(btn);
layout.getChildren().add(userField);
btn.setOnAction(e ->{
input = userField.getText();
window.close();
System.out.println(input);
});
layout.setAlignment(Pos.CENTER);
window.show();
return input;
}
}
I tried googling it but i cant really seem to understand the solution provided by others.
Its all good, figured it out after tinkering with it a little longer.
in the main class, i had to change window.show(); to window.showAndWait();

Snapshot Image can't be used as stage icon

I'm trying to set an image from a stage snapshot as the stage icon.
The following code demonstrates it:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.io.IOException;
public class IconTest extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(new StackPane(new ImageView(generateIcon(32))));
primaryStage.setScene(scene);
primaryStage.getIcons().setAll(generateIcon(32));
primaryStage.show();
}
private static Image generateIcon(int dimension) throws IOException {
Pane root = new Pane();
root.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
Scene scene = new Scene(root, dimension, dimension);
return scene.snapshot(new WritableImage(dimension, dimension));
}
}
Result:
As you can see, the stage icon is not a red square. Why is that? Loading from a file always works, generating the image using AWT works, too.
Workaround:
BufferedImage bimg = new BufferedImage(dimension, dimension, BufferedImage.TYPE_INT_ARGB);
SwingFXUtils.fromFXImage(scene.snapshot(new WritableImage(dimension, dimension)), bimg);
SwingFXUtils.toFXImage(bimg, new WritableImage(dimension, dimension));

Create a webview from a button in JavaFX

I have the following simple code where I have an horizontal block where there are two buttons, the remain part is empty. Now, I would like to view a Webview inside the free part after the user press the "current" button. I already did something similar but I wasn't able to manage the layout because it was a Jframe + JavaFX. I thought to re-build totally in JavaFx , for this reason I want to put the webview in a jfxPanel(I think it's the best solution, any other suggest is really appreciate). I should see the webview when I press the button , in this case current, for this reason I create an handle. So, How can I do it?
Thanks in advance
import java.util.ArrayList;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Launcher extends Application {
public static void main(String[] args) {
launch(Launcher.class, args);
}
Button buttonCurrent;
static HBox web;
static MyBrowser browser_lau;
public void start(Stage stage) {
BorderPane border = new BorderPane();
HBox hbox = addHBox();
border.setTop(hbox);
Scene scene = new Scene(border);
stage.setScene(scene);
stage.setTitle("Layout Sample");
stage.show();
buttonCurrent.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
final JFXPanel jfxPanel = new JFXPanel();
//border.setStyle("-fx-background-color: #ff0000;");
}
});
}
public HBox addHBox() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10); // Gap between nodes
hbox.setStyle("-fx-background-color: #336699;");
buttonCurrent = new Button("Current");
buttonCurrent.setPrefSize(100, 20);
Button buttonProjected = new Button("Projected");
buttonProjected.setPrefSize(100, 20);
hbox.getChildren().addAll(buttonCurrent, buttonProjected);
return hbox;
}
}
}
Here is some sample code to get you started, it creates a couple of WebViews and toggles between them depending upon a radio button selection.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class CountThePeople extends Application {
private static final String CURRENT_URL =
"http://www.census.gov/popclock/";
private static final String PROJECTED_URL =
"http://www.sciencedaily.com/releases/2015/08/150810110634.htm";
#Override
public void start(final Stage stage) throws Exception {
WebView current = new WebView();
current.getEngine().load(CURRENT_URL);
WebView projected = new WebView();
projected.getEngine().load(PROJECTED_URL);
StackPane viewHolder = new StackPane();
ToggleGroup choice = new ToggleGroup();
RadioButton currentRadio = new RadioButton("Current");
currentRadio.setToggleGroup(choice);
RadioButton projectedRadio = new RadioButton("Projected");
projectedRadio.setToggleGroup(choice);
choice.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
if (projectedRadio == newValue) {
viewHolder.getChildren().setAll(projected);
} else {
viewHolder.getChildren().setAll(current);
}
});
choice.selectToggle(currentRadio);
VBox layout = new VBox(10, currentRadio, projectedRadio, viewHolder);
layout.setPadding(new Insets(10));
stage.setTitle("World Population");
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}

Resources