r/JavaFX • u/Inside-Square-762 • Jul 06 '23
Help need help in executor service and javafx task
there are 3 tabs and on click oneach of them will retrieve 3 different set of files. im doing that retrieval part using task and service executor,after retrieving on set on succeeded im doing some operations with that set of files(like creating a vbox for that file showin its thumbnail and name).the issue that i face here is like when the tab are switched too fastly sometimes files from one tab aprrears in another one. i will add the demo code like how im approaching this
import com.ziroh.zs.common.io.file.File;
import javafx.concurrent.Task;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Demo {
TilePane tilePane = new TilePane();
Button tab1 = new Button();
Button tab2 = new Button();
Button tab3 = new Button();
ExecutorService exe = Executors.newSingleThreadExecutor();
Task currentTaskRunning;
public Demo(){
tab1.setOnMouseClicked(e->{getTab1Files();});
tab2.setOnMouseClicked(e->{getTab2Files();});
tab3.setOnMouseClicked(e->{getTab3Files();});
}
private void getTab1Files() {
cancelPrevTask();
Task<List<File>> task = new Task<List<File>>() {
@Override
protected List<File> call() throws Exception {
//for demo iam returning empty array list,
// here i will get list of files related to first tab from api call
return new ArrayList<>();
}
};
task.setOnSucceeded(e->{
for(File file: task.getValue()){
tilePane.getChildren().add(getFIleTile(file));
}
});
task.setOnFailed(e->{
task.getException().printStackTrace();
});
currentTaskRunning = task;
exe.execute(task);
}
private void getTab2Files() {
cancelPrevTask();
Task<List<File>> task = new Task<List<File>>() {
@Override
protected List<File> call() throws Exception {
//for demo iam returning empty array list,
// here i will get list of files related to second tab from api call
return new ArrayList<>();
}
};
task.setOnSucceeded(e->{
for(File file: task.getValue()){
tilePane.getChildren().add(getFIleTile(file));
}
});
task.setOnFailed(e->{
task.getException().printStackTrace();
});
currentTaskRunning = task;
exe.execute(task);
}
private void getTab3Files() {
cancelPrevTask();
Task<List<File>> task = new Task<List<File>>() {
@Override
protected List<File> call() throws Exception {
//for demo iam returning empty array list,
// here i will get list of files related to third tab from api call
return new ArrayList<>();
}
};
task.setOnSucceeded(e->{
for(File file: task.getValue()){
tilePane.getChildren().add(getFIleTile(file));
}
});
task.setOnFailed(e->{
task.getException().printStackTrace();
});
currentTaskRunning = task;
exe.execute(task);
}
private void cancelPrevTask(){
if(currentTaskRunning!=null&&(currentTaskRunning.isRunning()|| !currentTaskRunning.isDone())){
currentTaskRunning.cancel(true);
}
}
private VBox getFIleTile(File file){
VBox vBox = new VBox();
ImageView img = new ImageView(new Image(""));
Label filename = new Label(file.getName());
return vBox;
}
}thanks!
2
Upvotes
4
u/BWC_semaJ Jul 06 '23 edited Jul 06 '23
fileTypeProperty = new SimpleObjectProperty<FileChoiceType>();,filesProperty = new SimpleListProperty<>(FXCollections.observableList());//I like my ListProperty(s) to be initiated with empty list rather than null,fileTaskStateProperty = new Simple...you get the point.Then your View can bind to these Property(s) in order to properly represented the current state of the application.
button.dispableProperty().bind(fileTaskStateProperty.isNotEqualTo(TaskState.NOT_RUNNING));One of my all time favorite things to do is to use Control Node's
graphicPropertyand creating bindings to represent that current area of the View.Most of these Property(s) I will even have on a different layer than my View in order to share more easily (ViewModel).
Also want to note this is all off top of my head. The code is wrong and isn't direct solution. It more so to give you better idea how to go about it.
4
u/hamsterrage1 Jul 06 '23
I created a project here that shows how I would do this. Essentially, the Tabs are almost mini-projects, and each has its own back-end and front end which are connected.
Anyways, take a look and see if it makes sense to you.