How does the graphic inside a Javafx ListCell catch MouseEvents? - javafx

I have a custom ListCell with a ImageView as the graphic. I want this ImageView to catch MouseEvents (like mouse_over, or mouse_click). But it does not catch any events.
The custom ListCell however catches events.
Is this unusual behaviour or do I need to pass the Events from the ListCell to its graphic (somehow)?
If I add a Button, it gets Mouseevents, strangely.
If you have just a link of a working example, I would happily crawl my way through it ;)
Thanks for your effort.
Class CustomCell:
package test;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ListCell;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
public class CustomCell extends ListCell<String>{
ImageView removeTrack;
public CustomCell(){
removeTrack = new ImageView("https://lh3.googleusercontent.com/-lbN1Ca63JPs/AAAAAAAAAAI/AAAAAAAAAAQ/smvshnyosS4/s46-c-k/photo.jpg");
removeTrack.setOnMouseClicked(e -> {
System.out.println("test");
});
}
#Override
protected void updateItem(String t, boolean bln) {
super.updateItem(t, bln);
if (t != null) {
setText(t);
setGraphic(removeTrack);
setContentDisplay(ContentDisplay.RIGHT);
addEventFilter(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
removeTrack.setVisible(true);
}
});
addEventFilter(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
removeTrack.setVisible(false);
}
});
}
}
}
Main-Class:
package test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
import javafx.util.Callback;
public class JAVAtest extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
ObservableList<String> names = FXCollections.observableArrayList(
"Julia", "Ian", "Sue", "Matthew", "Hannah", "Stephan", "Denise");
ListView lv = new ListView(names);
lv.setCellFactory(new Callback<ListView<String>, ListCell<String>>(){
#Override
public ListCell<String> call(ListView<String> p) {
return new CustomCell();
}
});
Scene scene = new Scene(lv);
stage.setScene(scene);
stage.show();
}
}

Well.. I can't see why your code isn't working. Seems like a bug.
It works fine if you wrap the image view in a container of some kind. For example:
import javafx.event.EventHandler;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ListCell;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
public class CustomCell extends ListCell<String> {
ImageView removeTrack;
StackPane imageContainer ;
public CustomCell() {
removeTrack = new ImageView(
"https://lh3.googleusercontent.com/-lbN1Ca63JPs/AAAAAAAAAAI/AAAAAAAAAAQ/smvshnyosS4/s46-c-k/photo.jpg");
removeTrack.setOnMouseClicked(e -> {
System.out.println("test");
});
imageContainer = new StackPane(removeTrack);
}
#Override
protected void updateItem(String t, boolean bln) {
super.updateItem(t, bln);
if (t != null) {
setText(t);
setGraphic(imageContainer);
setContentDisplay(ContentDisplay.RIGHT);
addEventFilter(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
imageContainer.setVisible(true);
}
});
addEventFilter(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
imageContainer.setVisible(false);
}
});
} else {
setText(null);
setGraphic(null);
}
}
}
(As an aside: always handle all possible cases in the updateItem(...) method. Your code will have bugs if you start removing items or possibly while scrolling, because you don't handle the empty cell / null item case.)

Related

JavaFX custom TreeView with ComboBox

I try to create a TreeView that will have on every branch and leaf a ComboBox, an "add child" Button and an "remove child" Button.
Here is my code
package example;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* #author me
*/
public class Example extends Application {
String[] companyNames = new String[10];
#Override
public void start(Stage primaryStage) {
companyNames[0]="FORD";
companyNames[1]="MG";
companyNames[2]="LADA";
companyNames[3]="BMW";
companyNames[4]="KIA";
companyNames[5]="HYUNDAI";
companyNames[6]="AUDI";
companyNames[7]="MERCEDES";
companyNames[8]="DACIA";
companyNames[9]="TOYOTA";
final TreeItem<String> treeRootItem = new TreeItem<>("Root");
final TreeView<String> treeView = new TreeView<>(treeRootItem);
treeRootItem.setExpanded(true);
treeView.setCellFactory(e -> new CustomCell());
VBox mainVBox = new VBox();
mainVBox.getChildren().add(treeView);
Scene scene1 = new Scene(mainVBox,1600,600);
primaryStage.setScene(scene1);
primaryStage.show();
primaryStage.centerOnScreen();
}
//function custom TreeCell CustomCell
class CustomCell extends TreeCell<String> {
#Override
protected void updateItem(String item, boolean empty)
{
ComboBox companiesComboBox = new ComboBox();
Button addChildButton = new Button("Add Child");
Button removeButton = new Button("Remove Child");
super.updateItem(item, empty);
if (isEmpty()) {
setGraphic(null);
setText(null);
}
else {
HBox cellBox = new HBox(10);
if (item.equals("Root"))
{
cellBox.getChildren().addAll(companiesComboBox,addChildButton);
}
else cellBox.getChildren().addAll(companiesComboBox,addChildButton,removeButton);
setGraphic(cellBox);
setText(null);
}
companiesComboBox.getItems().addAll((Object[]) companyNames);
addChildButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
String selection=companiesComboBox.getSelectionModel().getSelectedItem().toString();
TreeItem<String> childNode1 = new TreeItem<>("Child Node");
getTreeItem().getChildren().add(childNode1);
}});
removeButton.setOnAction(value -> {
//somehow i will remove the leaf/branch
});
}
}
public static void main(String[] args) {
launch(args);
}
}
(As Alexandra pointed out, in my prior post, i had many Java Naming Conventions wrong, i hope i fixed it now.
A am sorry that i deleted my prior post but it was off topic with many wrongs so i decided to make a small research how to make a post with better code and more specific without mistakes and come back a few days later.I hope that now is presentable.)
The problem that i have is that when i add a new child , the hole tree loses the ComboBox selection.
Here is a picture of the project running https://ibb.co/8MVDqhX
Any help/suggestion will be appreciated
UPDATE:
After Slaw answer i tried to create a Model Class named SelectedCompanyClass
package example;
import javafx.beans.property.StringProperty;
/**
*
* #author me
*/
public class SelectedCompanyClass {
public String companyNameString;
public SelectedCompanyClass(String companyNameString) {
this.companyNameString = companyNameString;
}
}
And i changed my main class like this
package example;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* #author me
*/
public class Example extends Application {
String[] companyNames = new String[10];
int x=0;
SelectedCompanyClass SelectedCompanyClass1;
#Override
public void start(Stage primaryStage) {
this.SelectedCompanyClass1 = new SelectedCompanyClass("root");
companyNames[0]="FORD";
companyNames[1]="MG";
companyNames[2]="LADA";
companyNames[3]="BMW";
companyNames[4]="KIA";
companyNames[5]="HYUNDAI";
companyNames[6]="AUDI";
companyNames[7]="MERCEDES";
companyNames[8]="DACIA";
companyNames[9]="TOYOTA";
final TreeItem<SelectedCompanyClass> treeRootItem = new TreeItem<>(SelectedCompanyClass1);
final TreeView<SelectedCompanyClass> treeView = new TreeView<>(treeRootItem);
treeRootItem.setExpanded(true);
treeView.setCellFactory(e -> new CustomCell());
VBox mainVBox = new VBox();
mainVBox.getChildren().add(treeView);
Scene scene1 = new Scene(mainVBox,1600,600);
primaryStage.setScene(scene1);
primaryStage.show();
primaryStage.centerOnScreen();
System.out.println("Start runs");
}
class CustomCell extends TreeCell<SelectedCompanyClass> {
protected void updateItem(SelectedCompanyClass SelectedCompanyClass1, boolean empty)
{
ComboBox companiesComboBox = new ComboBox();
Button addChildButton = new Button("Add Child");
Button removeButton = new Button("Remove Child");
super.updateItem(SelectedCompanyClass1,empty);
if (isEmpty()) {
setGraphic(null);
setText(null);
}
else {
HBox cellHBox = new HBox();
if (SelectedCompanyClass1!=null)
{
if (SelectedCompanyClass1.companyNameString.equals("Root"))
{
cellHBox.getChildren().addAll(companiesComboBox,addChildButton);
}
else cellHBox.getChildren().addAll(companiesComboBox,addChildButton,removeButton);
companiesComboBox.getSelectionModel().select(SelectedCompanyClass1.companyNameString);
}
setGraphic(cellHBox);
setText(null);
}
companiesComboBox.getItems().addAll((Object[]) companyNames);
addChildButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
String selection="none";
SelectedCompanyClass SelectedCompanyClass2 = new SelectedCompanyClass(selection);
if (companiesComboBox.getSelectionModel().getSelectedItem().toString()!=null) {
selection=companiesComboBox.getSelectionModel().getSelectedItem().toString();
SelectedCompanyClass2.companyNameString=selection;
}
TreeItem<SelectedCompanyClass> childNode2 = new TreeItem<>(SelectedCompanyClass2);
getTreeItem().getChildren().add(childNode2);
childNode2.setExpanded(true);
}});
removeButton.setOnAction(value -> {
//somehow i will remove the leaf/branch
});
}
}
public static void main(String[] args) {
launch(args);
}
}
Now, thanks to Slaw , i have made progress and i create new objects TreeItem as childs, but parent Node loses its ComboBox selection
UPDATE 2:
After the usefull suggestions, i finally done it, here is the code
The model Class code
package example;
import javafx.beans.property.StringProperty;
public class SelectedCompanyClass {
String companyNameString = new String();
public SelectedCompanyClass() {
companyNameString="Choose";
}
}
and the Main code
package example;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* #author me
*/
public class Example extends Application {
String[] companyNames = new String[10];
int x=0;
#Override
public void start(Stage primaryStage) {
SelectedCompanyClass SelectedCompanyClass1 = new SelectedCompanyClass();
companyNames[0]="FORD";
companyNames[1]="MG";
companyNames[2]="LADA";
companyNames[3]="BMW";
companyNames[4]="KIA";
companyNames[5]="HYUNDAI";
companyNames[6]="AUDI";
companyNames[7]="MERCEDES";
companyNames[8]="DACIA";
companyNames[9]="TOYOTA";
final TreeItem<SelectedCompanyClass> treeRootItem = new TreeItem<>(SelectedCompanyClass1);
final TreeView<SelectedCompanyClass> treeView = new TreeView<>(treeRootItem);
treeRootItem.setExpanded(true);
treeView.setCellFactory(e -> new CustomCell());
VBox mainVBox = new VBox();
mainVBox.getChildren().add(treeView);
Scene scene1 = new Scene(mainVBox,1600,600);
primaryStage.setScene(scene1);
primaryStage.show();
primaryStage.centerOnScreen();
System.out.println("Start runs");
}
class CustomCell extends TreeCell<SelectedCompanyClass> {
protected void updateItem(SelectedCompanyClass SelectedCompanyClass1, boolean empty)
{
ComboBox companiesComboBox = new ComboBox();
Button addChildButton = new Button("Add Child");
Button removeButton = new Button("Remove");
Label objectLabel = new Label();
companiesComboBox.valueProperty().addListener((obs, oldValue, newValue) -> {
SelectedCompanyClass1.companyNameString=newValue.toString();
});
super.updateItem(SelectedCompanyClass1,empty);
if (isEmpty()) {
setGraphic(null);
setText(null);
}
else {
HBox cellHBox = new HBox();
if (SelectedCompanyClass1!=null)
{
cellHBox.getChildren().addAll(companiesComboBox,addChildButton,removeButton,objectLabel);
companiesComboBox.getSelectionModel().select(SelectedCompanyClass1.companyNameString);
}
setGraphic(cellHBox);
setText(null);
}
companiesComboBox.getItems().addAll((Object[]) companyNames);
addChildButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
String selection="none";
SelectedCompanyClass SelectedCompanyClass2 = new SelectedCompanyClass();
TreeItem<SelectedCompanyClass> childNode2 = new TreeItem<>(SelectedCompanyClass2);
getTreeItem().getChildren().add(childNode2);
childNode2.setExpanded(true);
companiesComboBox.valueProperty().addListener((obs, oldValue, newValue) -> {
SelectedCompanyClass2.companyNameString=newValue.toString();
});
}});
removeButton.setOnAction(value -> {
//somehow i will remove the leaf/branch
});
}
}
public static void main(String[] args) {
launch(args);
}
}
Best regards to all

Error with handle(Event) in EventHandler

I'm new new to javafx and I would like just to print the the coordinates of the mouse click.
I've seen examples but I've got an error in the EventHandler line that I don't understand, it says:
anonymous javafxapplication2.JavaFXApplication2$1 is not abstract and does not override abstract method handle(Event) in EventHandler
What's the problem? Thanks!
package javafxapplication2;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class JavaFXApplication2 extends Application {
#Override
public void start(Stage primaryStage) {
Line line = new Line();
Group root = new Group(line);
Scene scene = new Scene(root, 800, 800);
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() {
#Override
public void handle(MouseEvent event) {
System.out.println(event.getX());
System.out.println(event.getY());
}
}
primaryStage.setTitle("Disegna linee");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
From looking at the documentation here on javafx handlers I believe your problem is that you need to provide the type to the new eventHandler like
scene.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() { // Was missing the <MouseEvent>
#Override
public void handle(MouseEvent event) { ... };
});

is there any way to add button into dropdown list with textfields using controlfx jar

i have a textfield with autocomplete and i want to show button near each item appeared into that list
this is my code
List<String> s = ms.getUsernames(ms.getUser(7).getListamis());
TextFields.bindAutoCompletion(txtsearch, s);
this is the method getusernames
public List<String> getUsernames(String list_amis) {
List<String> ls = new ArrayList<>();
String [] idmember = list_amis.split("/");
for (String i : idmember) {
ls.add(getUser(Integer.parseInt(i)).getUsername());
}
return ls;
}
this is my output
i want to add a button near testing as a result i can get its ID
This is the solution I came up with. There certainly is a better way that is more elegant and works better, but I guess this would work for many situations. You would have to do something about the size of the ListView, though, and I didn't test this in an environment in which the appearing ListView might change something about the design of the rest of the UI. I suggest putting the whole thing into a PopOver or something.
import java.util.function.Predicate;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class NewFXMain extends Application {
#Override
public void start(Stage primaryStage) {
ObservableList<String> list = FXCollections.observableArrayList("one","two","three");
FilteredList<String> filteredList = new FilteredList<>(list);
VBox box = new VBox();
TextField textField = new TextField();
textField.textProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
filteredList.setPredicate(new Predicate<String>() {
#Override
public boolean test(String s){
return s.toLowerCase().contains(newValue.toLowerCase());
}
});
}
});
ListView listView = new ListView();
listView.setItems(filteredList);
listView.visibleProperty().bind(textField.textProperty().isNotEmpty());
listView.setCellFactory(new Callback() {
#Override
public Object call(Object param) {
ListCell cell = new ListCell(){
#Override
public void updateItem(Object item, boolean empty){
if(item != null && !empty){
super.updateItem(item, empty);
HBox contentBox = new HBox();
Label label = new Label(item.toString());
Button button = new Button("delete");
HBox separator = new HBox();
HBox.setHgrow(separator, Priority.ALWAYS);
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println(item);
}
});
contentBox.getChildren().addAll(label, separator, button);
setGraphic(contentBox);
}else{
setText(null);
setGraphic(null);
}
}
};
return cell;
}
});
box.getChildren().addAll(textField,listView);
StackPane root = new StackPane();
root.getChildren().add(box);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Autocomplete");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX application won't compile in NetBeans IDE 8.1

I am trying to run this application in which I used event-handling and it won't compile and won't show anything. I am currently using NetBeans 8.1. What is that I did wrong. Also it is not showing any errors.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class event extends Application {
public void start(Stage primaryStage)
{
HBox pane = new HBox();
pane.setAlignment(Pos.CENTER);
Button btOk = new Button("OK");
Button btCancel = new Button("Cancel");
OkHandlerClass handler1 = new OkHandlerClass();
btOk.setOnAction(handler1);
CancelHandlerClass handler2 = new CancelHandlerClass();
pane.getChildren().addAll(btOk, btCancel);
Scene scene = new Scene(pane);
primaryStage.setTitle("Handle the fucking event");
primaryStage.setScene(scene);
primaryStage.show();
}
class OkHandlerClass implements EventHandler<ActionEvent>{
public void handle(ActionEvent e) {
System.out.println("OK button clicked");
}
}
class CancelHandlerClass implements EventHandler<ActionEvent>{
public void handle(ActionEvent event) {
System.out.println("Cancel button clicked");
}
}
public static void main(String[] args) {
launch(args);
}
}

Get list of all JavaFX Tasks

Is there a way to get all running Tasks and Services in JavaFX application?
I would like to display the running Tasks in a List.
brian's answer is the way to go. If you have multiple places you are creating Tasks, this might get a bit tricky to manage. The following shows a way to encapsulate everything into an implementation of Executor. Note that I'm still doing exactly as brian suggests: adding Tasks to a List and removing them when they're complete; I just manage it all in an Executor so that the code to modify the list is all in one place.
MonitoringExecutor.java:
import java.util.concurrent.Executor;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
/**
* Wraps an Executor and exposes an ObservableList of Workers which have been
* executed but have not completed. (Workers are considered completed if they
* exit a RUNNING state; i.e. they are in a SUCCEEDED, FAILED, or CANCELLED state.)
*
*/
public class MonitoringExecutor implements Executor {
private final Executor exec ;
private final ObservableList<Worker<?>> taskList ;
public MonitoringExecutor(Executor exec) {
this.exec = exec;
this.taskList = FXCollections.observableArrayList();
}
#Override
public void execute(Runnable command) {
if (command instanceof Worker) {
final Worker<?> task = (Worker<?>) command ;
task.stateProperty().addListener(new ChangeListener<State>() {
#Override
public void changed(ObservableValue<? extends State> obs,
State oldState, State newState) {
if (oldState == State.RUNNING) {
taskList.remove(task);
}
}
});
taskList.add(task);
}
exec.execute(command);
}
public ObservableList<Worker<?>> getWorkerList() {
return taskList;
}
}
And here's an example of using it:
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
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.control.ProgressBar;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TaskMonitor extends Application {
#Override
public void start(Stage primaryStage) {
final IntegerProperty tasksCreated = new SimpleIntegerProperty(0);
final ThreadFactory threadFactory = new ThreadFactory() {
#Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
};
final MonitoringExecutor exec = new MonitoringExecutor(Executors.newFixedThreadPool(5, threadFactory));
final TableView<Worker<?>> taskTable = createTable();
taskTable.setItems(exec.getWorkerList());
final Button newTaskButton = new Button();
newTaskButton.textProperty().bind(Bindings.format("Create task %d", tasksCreated.add(1)));
newTaskButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
tasksCreated.set(tasksCreated.get()+1);
exec.execute(new CountingTask("Task "+tasksCreated.get()));
}
});
final BorderPane root = new BorderPane();
root.setCenter(taskTable);
final HBox controls = new HBox();
controls.setPadding(new Insets(10));
controls.setAlignment(Pos.CENTER);
controls.getChildren().add(newTaskButton);
root.setBottom(controls);
final Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private TableView<Worker<?>> createTable() {
final TableView<Worker<?>> taskTable = new TableView<>();
final TableColumn<Worker<?>, String> titleCol = new TableColumn<>("Title");
titleCol.setCellValueFactory(new PropertyValueFactory<Worker<?>, String>("title"));
final TableColumn<Worker<?>, Double> progressCol = new TableColumn<>("Progress");
progressCol.setCellValueFactory(new PropertyValueFactory<Worker<?>, Double>("progress"));
progressCol.setCellFactory(new Callback<TableColumn<Worker<?>, Double>, TableCell<Worker<?>, Double>>() {
#Override
public TableCell<Worker<?>, Double> call(TableColumn<Worker<?>, Double> col) {
return new ProgressTabelCell();
}
});
final TableColumn<Worker<?>, State> stateCol = new TableColumn<>("State");
stateCol.setCellValueFactory(new PropertyValueFactory<Worker<?>, State>("state"));
final TableColumn<Worker<?>, String> messageCol = new TableColumn<>("Message");
messageCol.setCellValueFactory(new PropertyValueFactory<Worker<?>, String>("message"));
messageCol.setPrefWidth(200);
taskTable.getColumns().addAll(Arrays.asList(titleCol, progressCol, stateCol, messageCol));
return taskTable;
}
private static class CountingTask extends Task<Void> {
private CountingTask(String title) {
updateTitle(title);
}
#Override
protected Void call() throws Exception {
final int n = new Random().nextInt(100)+100;
for (int i=0; i<n; i++) {
updateProgress(i, n);
updateMessage(String.format("Count is %d (of %d)", i, n));
Thread.sleep(100);
}
return null;
}
}
private static class ProgressTabelCell extends TableCell<Worker<?>, Double> {
final ProgressBar progressBar = new ProgressBar();
#Override
public void updateItem(Double value, boolean empty) {
if (empty || value == null) {
setGraphic(null);
} else {
setGraphic(progressBar);
progressBar.setProgress(value);
}
}
}
public static void main(String[] args) {
launch(args);
}
}
Just add and remove them from a list as needed. Then you can show them in a list. Here's some pieces of code.
ObservableList<String> runningTasks;
runningTasks.add(task.getTitle());
new Thread(task).start();
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent event) {
runningTasks.remove(task.getTitle());
}});

Resources