Disable the Cancel Button in Progress Bar in JavaFx - javafx

I have made a progress bar in javafx. There is a cancel Button by default. I just want to disable this cancel button when my task got completed.
jobProgressView.setGraphicFactory(task -> {
return new Button("save");
});

Without more code, I'm only able to make a guess. Even your added code isn't enough to know all things from your implementation.
So this solution assumes, that you have a Task that is running and showing it's progress on a Progressbar. The Task here is wrapped in a service, which can be restarted (maybe you also need this?).
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class CancelButtonDemo extends Application {
Service<Integer> service = new MyService();
#Override
public void start(Stage primaryStage) {
Button start = new Button();
Button cancel = new Button();
ProgressBar progress = new ProgressBar(0);
start.setText("Run Task");
start.setOnAction((ActionEvent event) -> {
if (!(service.getState().equals(Worker.State.READY))) {
service.reset();
}
progress.progressProperty().bind(service.progressProperty());
service.start();
});
start.disableProperty().bind(service.runningProperty());
cancel.setText("Cancel Task");
cancel.setOnAction((ActionEvent event) -> {
service.cancel();
progress.progressProperty().unbind();
progress.setProgress(0);
});
cancel.disableProperty().bind(Bindings.not(service.runningProperty()));
VBox root = new VBox(20);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(start, progress, cancel);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Cancel Button Demo");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
class MyService extends Service<Integer> {
#Override
protected Task<Integer> createTask() {
return new Task<Integer>() {
#Override
protected Integer call() throws Exception {
int iterations;
for (iterations = 0; iterations < 10000000; iterations++) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
updateMessage("Iteration " + iterations);
updateProgress(iterations, 10000000);
}
return iterations;
}
};
}
}
}
The above application looks like this:

Related

use vlcj-javafx-demo develop a player, but it looks some components UI not update correctly when set full screen

I try to use vlcj-javafx-demo to develop a video player, and I put the progress bar(Slider) on the StackPane over the video layer. In the beginning, it looks work well, but when I set maximum or full screen the app, it looks some components UI did not update correctly. How can I correct it?
Thanks a lot!
normally:
[1]: https://i.stack.imgur.com/bbE51.png
normally:
[2]: https://i.stack.imgur.com/Plsb1.png
the red color is the sence background color.
the code :
package my.javafx.myplayer;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.Slider;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import java.util.ArrayList;
import java.util.List;
import static uk.co.caprica.vlcj.javafx.videosurface.ImageViewVideoSurfaceFactory.videoSurfaceForImageView;
/**
*
*/
public class VlcjJavaFxApplication extends Application {
private final MediaPlayerFactory mediaPlayerFactory;
private final EmbeddedMediaPlayer embeddedMediaPlayer;
private ImageView videoImageView;
Slider progress=new Slider();
public VlcjJavaFxApplication() {
this.mediaPlayerFactory = new MediaPlayerFactory();
this.embeddedMediaPlayer = mediaPlayerFactory.mediaPlayers().newEmbeddedMediaPlayer();
this.embeddedMediaPlayer.events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
#Override
public void mediaPlayerReady(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
Platform.runLater(()->{
progress.setValue(0);
progress.setMax(embeddedMediaPlayer.media().info().duration());
progress.setMin(0);
});
}
#Override
public void playing(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
}
#Override
public void paused(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
}
#Override
public void stopped(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
}
#Override
public void timeChanged(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer, long newTime) {
Platform.runLater(()->{
progress.setValue(newTime);
});
}
});
}
#Override
public void init() {
this.videoImageView = new ImageView();
this.videoImageView.setPreserveRatio(true);
embeddedMediaPlayer.videoSurface().set(videoSurfaceForImageView(this.videoImageView));
}
#Override
public final void start(Stage primaryStage) throws Exception {
List<String> params = new ArrayList<String>();
params.add("/Users/baixq/Downloads/妙味课堂xhtml+css2/妙味课堂-XHTMLCSS2整站视频教程-4.avi");
if (params.size() != 1) {
System.out.println("Specify a single MRL");
System.exit(-1);
}
StackPane root=new StackPane();
//BorderPane root = new BorderPane();
root.setStyle("-fx-background-color: black;");
videoImageView.fitWidthProperty().bind(root.widthProperty());
videoImageView.fitHeightProperty().bind(root.heightProperty());
root.widthProperty().addListener((observableValue, oldValue, newValue) -> {
// If you need to know about resizes
});
root.heightProperty().addListener((observableValue, oldValue, newValue) -> {
// If you need to know about resizes
});
Scene scene = new Scene(root, 1200, 675, Color.RED);
primaryStage.setTitle("vlcj JavaFX");
primaryStage.setScene(scene);
AnchorPane contrlBox=new AnchorPane();//操作面板上的控制模块
contrlBox.prefWidthProperty().bind(root.widthProperty());
contrlBox.prefHeightProperty().bind(root.heightProperty().multiply(0.1));
BorderPane controlBar=new BorderPane();
controlBar.setStyle("-fx-background-color: #130c0e;");
controlBar.prefWidthProperty().bind(root.widthProperty());
controlBar.prefHeightProperty().bind(root.heightProperty().multiply(0.1));
controlBar.setCenter(progress);
Button fullScreen=new Button("全屏");
controlBar.setRight(fullScreen);
contrlBox.getChildren().add(controlBar);
contrlBox.setBottomAnchor(controlBar, 0.0);
root.getChildren().addAll(videoImageView,contrlBox);
primaryStage.show();
embeddedMediaPlayer.media().play(params.get(0));
fullScreen.setOnAction(event->{
primaryStage.setFullScreen(true);
});
root.setOnMouseEntered(event->{
Platform.runLater(()->{
FadeTransition ft = new FadeTransition(Duration.millis(500), contrlBox);
ft.setFromValue(0.0);
ft.setToValue(1);
//ft.setCycleCount(Timeline.INDEFINITE);
ft.setAutoReverse(false);
ft.play();
contrlBox.setVisible(true);
});
});
root.setOnMouseExited(event->{
Platform.runLater(()->{
FadeTransition ft = new FadeTransition(Duration.millis(500), contrlBox);
ft.setFromValue(1);
ft.setToValue(0.0);
ft.setAutoReverse(false);
ft.play();
contrlBox.setVisible(false);
});
});
//embeddedMediaPlayer.controls().setPosition(0.4f);
}
#Override
public final void stop() {
embeddedMediaPlayer.controls().stop();
embeddedMediaPlayer.release();
mediaPlayerFactory.release();
}
public static void main(String[] args) {
launch(args);
}
}
You appear to be using a Linux OS, try passing one or more of these system properties when you start your JVM:
-Dprism.dirtyopts=false
-Dprism.forceUploadingPainter=true
This is mentioned under "Linux notes" here: https://github.com/caprica/vlcj-javafx-demo/tree/vlcj-5.x
I have seen similar painting glitches on Linux before and in all cases using these properties, at least for me, clears the issue with only a small hit to performance - even when doing something like a full-screen grid of nine concurrent media players, where each one had an animating video controls overlay.

Best way to synchronize countdowntimers in JavaFX

My Programm needs nine Countdowntimers. The timers are started by the user. In my implementation I create a timerclasses for each timer started. The timerclass uses a timeline. Depending on the start of the timers the seconds are asynchrone.
I am not sure how to proceed.
My first thought were to use only 1 timeline for all countdowns. I would put all stringProperties into a list and the timeline will change the property. I am not so sure if this is a good way?
With some google I found out that there is animationtimer which could be used for such a problem. But I couldn't understand the examples. I have to overwrite the handle method. How should I update my timer with this?
The idea is correct: use one animation tool such as PauseTransition or TimeLine (1) to update all counters as demonstrated in the following MRE:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class SyncedCounters extends Application {
private static final int MAX_COUNT = 100;
private Map<Label, Integer> counters;
private VBox countersPane;
#Override public void start(Stage stage) throws IOException {
counters = new HashMap<>();
countersPane = new VBox();
Button addCounter = new Button("Add Counter");
addCounter.setOnAction(e->addCounter());
BorderPane root = new BorderPane(countersPane, null, null, null, addCounter);
stage.setScene(new Scene(new ScrollPane(root),250,200));
stage.show();
update();
}
private void update() {
PauseTransition pause = new PauseTransition(Duration.seconds(1));
pause.setOnFinished(event ->{
updateCounters();
pause.play();
});
pause.play();
}
private void addCounter() {
Label label = new Label(String.valueOf(MAX_COUNT));
label.setAlignment(Pos.CENTER);
label.setPrefSize(150, 25);
counters.put(label, MAX_COUNT);
countersPane.getChildren().add(label);
}
private void updateCounters() {
for(Label l : counters.keySet()){
int counterValue = counters.get(l);
if(counterValue > 0 ){
counterValue--;
l.setText(String.valueOf(counterValue));
counters.put(l, counterValue);
}
}
}
public static void main(String[] args) {
launch(args);
}
}
(1) To use TimeLine instead of PauseTransition change update() to :
void update() {
Timeline timeline = new Timeline();
timeline.setCycleCount(Animation.INDEFINITE);
KeyFrame keyFrame = new KeyFrame(
Duration.seconds(1),
event -> {updateCounters();}
);
timeline.stop();
timeline.getKeyFrames().clear();
timeline.getKeyFrames().add(keyFrame);
timeline.play();
}

How to set Alert box position over current primaryStage? (JavaFX)

EDIT:
I have an alert box that pops up if the user clicks "Delete" for removing an item in a ListView. It works, but I would like it to pop over the original stage. It showed up in my first monitor. Is there any way to set the position of the alert when it's shown?
Note, the "owner" is in a different class, and I created everything with Scenebuilder/FXML. I cannot figure out how to get initOwner() to work. Here is the "Main" class:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Assignment_5 extends Application {
public Stage primaryStage;
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Assignment_5.fxml"));
primaryStage.setTitle("Plant Pack");
primaryStage.setScene(new Scene(root, 1200, 500));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is the working code within the Controller class. It's not necessary to implement the modality of this alert, but it would be a nice addition to make it more convenient. I simply don't know how to pass the main Window from the Main class to this:
protected void handleDeleteButtonClick(ActionEvent event) {
Alert alertBox = new Alert(Alert.AlertType.CONFIRMATION, "Confirm Delete", ButtonType.OK, ButtonType.CANCEL);
alertBox.setContentText("Are you sure you want to delete this " + plantType.getValue().toString().toLowerCase() + "?");
alertBox.showAndWait();
if(alertBox.getResult() == ButtonType.OK) {
int selectedPlant = plantList.getSelectionModel().getSelectedIndex();
observablePlantList.remove(selectedPlant);
}
else {
alertBox.close();
}
}
I understand this is fairly new, so it's difficult to find many resources. If anyone knows any info I may have missed, please let me know. Thanks for any help offered.
I am using Java 8 with IntelliJ 14.1.5.
As #jewelsea suggests, setting the modality and owner for the alert box will assure that the alert will appear over the stage, even if the stage is moved.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class DeleteAlertDemo extends Application {
Stage owner;
ObservableList<String> observablePlantList;
ListView<String> plantList;
protected void handleDeleteButtonClick(ActionEvent event) {
String item = plantList.getSelectionModel().getSelectedItem();
Alert alertBox = new Alert(Alert.AlertType.CONFIRMATION, "Confirm Delete",
ButtonType.OK, ButtonType.CANCEL);
alertBox.setContentText("Are you sure you want to delete this "
+ item.toLowerCase() + "?");
alertBox.initModality(Modality.APPLICATION_MODAL); /* *** */
alertBox.initOwner(owner); /* *** */
alertBox.showAndWait();
if (alertBox.getResult() == ButtonType.OK) {
int selectedPlant = plantList.getSelectionModel().getSelectedIndex();
observablePlantList.remove(selectedPlant);
} else {
alertBox.close();
}
}
#Override
public void start(Stage primaryStage) {
owner = primaryStage; /* *** */
Button deleteBtn = new Button();
deleteBtn.setText("Delete");
deleteBtn.setOnAction(this::handleDeleteButtonClick);
observablePlantList = FXCollections.observableArrayList("Begonia",
"Peony", "Rose", "Lilly", "Chrysanthemum", "Hosta");
plantList = new ListView<>(observablePlantList);
plantList.getSelectionModel().select(0);
BorderPane root = new BorderPane();
root.setCenter(plantList);
root.setRight(deleteBtn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Delete Alert Demo");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

JavaFX - Control and Concurrency

I have a sample Hello World JavaFx. I am using Eclipse and eFxclipse plugin. My Eclipse is kepler which is Eclipse 4.3.2 version and Java servion is Jdk1.7-045.
What I try to add is very little concurrency codes, I just want to update button text in the example. Could this backend task interact with upfront UI control, for example button, scene? If not, how could I make tailored backend task, then interact with UI control?
Thanks in advance
package com.juhani.fx.exer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application{
private static final short COREPOOLSIZE=2;
private static final short MAXIMUMPOOLSIZE=2;
private static final int WORKQUEUECAPACITY=100;
private static Logger log = LogManager.getLogger(
HelloWorld.class.getName());
private ExecutorService executors = new ThreadPoolExecutor(COREPOOLSIZE,MAXIMUMPOOLSIZE,20,TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(WORKQUEUECAPACITY));
public static void main(String[] args) {
LogMessage logMessage = new LogMessage("BEGIN",1.0,1,"HELLOWORLD");
log.trace(logMessage.toString());
launch(args);
}
#Override
public void start(final Stage primaryStage) throws InterruptedException {
primaryStage.setTitle("Hello World!");
final Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
final StackPane root = new StackPane();
root.getChildren().add(btn);
final Scene scene= new Scene(root,300,250);
primaryStage.setScene(scene);
primaryStage.show();
Task<Boolean> task = new Task<Boolean>() {
#Override
public Boolean call() {
for(int i=0;i<20;i++){
btn.setText("First row\nSecond row "+i);
primaryStage.show();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log.error(new LogMessage("entering interruption",1.0,2,"exception").toString());
}
}
return new Boolean(true);
}
};
executors.submit(task);
}
}
This answer specially talks about the use of Platform.runLater. If you are using Task, you are better off updating the UI using the method it provides as stated in
kleopatra's answer.
For updating the UI, you have to be on the Javafx thread.
Once you are on any other thread, use Platform.runLater() to update those data back to Javafx UI. A working example can be found below
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
private static final short COREPOOLSIZE = 2;
private static final short MAXIMUMPOOLSIZE = 2;
private static final int WORKQUEUECAPACITY = 100;
private ExecutorService executors = new
ThreadPoolExecutor(COREPOOLSIZE, MAXIMUMPOOLSIZE, 20, TimeUnit.MINUTES,
new ArrayBlockingQueue<Runnable>(WORKQUEUECAPACITY));
public static void main(String[] args) {
launch(args);
}
#Override
public void start(final Stage primaryStage) throws InterruptedException {
primaryStage.setTitle("Hello World!");
final Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
final StackPane root = new StackPane();
root.getChildren().add(btn);
final Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
Task<Boolean> task = new Task<Boolean>() {
#Override
public Boolean call() {
final AtomicInteger i = new AtomicInteger(0);
for( ; i.get() < 20; i.incrementAndGet()) {
Platform.runLater(new Runnable() {
#Override
public void run() {
btn.setText("First row\nSecond row " + i);
}
});
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {}
}
return Boolean.valueOf(true);
}
};
executors.submit(task);
}
}
For more information you can go through the links provided here
A Task is designed to interact with the ui on the fx-application thread, to take advantage of that support you should use it as designed :-)
As a general rule, you must not access ui in the call method [*] of the Task. Instead, update one of its properties (message, progress ...) and bind that property to your ui. Sample code:
Task<Boolean> taskWithBinding = new Task<Boolean>() {
#Override
public Boolean call() {
final AtomicInteger i = new AtomicInteger(0);
for( ; i.get() < 20; i.incrementAndGet()) {
updateMessage("First row\nSecond row " + i);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
};
btn.textProperty().bind(taskWithBinding.messageProperty());
[*] The one exception is outlined (wrap the access into an runLater) in the other answer. Doing so is technically correct - but then you are by-passing a Task's abilities and could use an arbitrary Runnable ...

using modality in a popup window javafx

I kept a popup dialog on a pane and it came at the top of the other components. Now I want to disable accessing all the other components of the program. How to do it?
The popup API does not have an initModality(Modality.APPLICATION_MODAL); method which is exactly what you want. In this case, you can make your popup window a stage and use the method mentioned above.
This is for #Xsleek in solution, example code:-
package popupexample;
import java.text.SimpleDateFormat;
import java.util.Date;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
*
* #author reegan
*/
public class PopUpExample extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
popupErrorMsg();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public void popupErrorMsg() {
final Stage myDialog = new Stage();
myDialog.initModality(Modality.APPLICATION_MODAL);
Button okButton = new Button("Ok");
okButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
myDialog.close();
}
});
Date todayDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Scene myDialogScene = new Scene(VBoxBuilder.create()
.children(new Text("Please Enter Validate Date \n \t "+ dateFormat.format(todayDate)), okButton)
.spacing(30)
.alignment(Pos.CENTER)
.padding(new Insets(10))
.build());
myDialog.setScene(myDialogScene);
myDialog.show();
}
}

Resources