Dynamically adding data in Hashmap to TableView in JavaFX - callback

I have a map mapping symbols to prices.I want to display a table with one column containing keys and the other column containing corresponding values in JavaFX
public class myMap {
Map<Symbol, Price> map;
}
I want to display a table like the following
Symbol | Price
I guess it can be done by using TableView with CallBack.

Since I've already done something similar to this I'll add my code.
import java.util.Map;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.converter.NumberStringConverter;
public class MapTable extends Application {
#Override
public void start(Stage primaryStage) {
final ObservableMap<String, Number> obsMap = FXCollections.observableHashMap();
for (int i = 0; i < 3; i++) obsMap.put("key "+i, i*10d);
final TableView<ObservableMap.Entry<String, Number>> tv = new TableView(FXCollections.observableArrayList(obsMap.entrySet()));
tv.setEditable(true);
obsMap.addListener((MapChangeListener.Change<? extends String, ? extends Number> change) -> {
tv.setItems(FXCollections.observableArrayList(obsMap.entrySet()));
});
TableColumn<ObservableMap.Entry<String, Number>,String> keyCol = new TableColumn<>("key");
TableColumn<ObservableMap.Entry<String, Number>,Number> priceCol = new TableColumn<>("price");
tv.getColumns().addAll(keyCol,priceCol);
keyCol.setCellValueFactory((p) -> {
return new SimpleStringProperty(p.getValue().getKey());
});
keyCol.setCellFactory(TextFieldTableCell.forTableColumn());
keyCol.setOnEditCommit((TableColumn.CellEditEvent<Map.Entry<String,Number>, String> t) -> {
final String oldKey = t.getOldValue();
final Number oldPrice = obsMap.get(oldKey);
obsMap.remove(oldKey);
obsMap.put(t.getNewValue(),oldPrice);
});
priceCol.setCellValueFactory((p) -> {
return new ReadOnlyObjectWrapper<>(p.getValue().getValue());
});
priceCol.setCellFactory(TextFieldTableCell.forTableColumn(new NumberStringConverter()));
priceCol.setOnEditCommit((TableColumn.CellEditEvent<Map.Entry<String,Number>, Number> t) -> {
obsMap.put(t.getTableView().getItems().get(t.getTablePosition().getRow()).getKey(),//key
t.getNewValue());//val);
});
Button btn1 = new Button();
btn1.setText("Add data");
btn1.setOnAction((ActionEvent event) -> {
obsMap.put("hi",100);
});
Button btn2 = new Button();
btn2.setText("verify data");
btn2.setOnAction((ActionEvent event) -> {
for (Map.Entry<String,Number> me : obsMap.entrySet())
System.out.println("key "+me.getKey()+" val "+me.getValue());
});
VBox root = new VBox(tv,btn1,btn2);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Map Table test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Hope this can help.
public class Demo {
public static void main(String[] args) {
// Use Java Collections to create the List.
Map<String,String> map = new HashMap<String,String>();
// Now add observability by wrapping it with ObservableList.
ObservableMap<String,String> observableMap = FXCollections.observableMap(map);
observableMap.addListener(new MapChangeListener() {
#Override
public void onChanged(MapChangeListener.Change change) {
System.out.println("Detected a change! ");
}
});
// Changes to the observableMap WILL be reported.
observableMap.put("key 1","value 1");
System.out.println("Size: "+observableMap.size());
// Changes to the underlying map will NOT be reported.
map.put("key 2","value 2");
System.out.println("Size: "+observableMap.size());
}
}
http://docs.oracle.com/javafx/2/collections/jfxpub-collections.htm

Related

Javafx Stage is blank for a few seconds before showing elements

I am trying to create a game with JavaFX. I have created 2 stages, one is for the splashscreen, the second one is for the actual game itself. This is my first ever JavaFX program, i am a student learning JavaFX. The problem i am having is that when i hide the first stage and show the second one, the second stage stays blank for a few seconds and then continues to show all element(s). For now, there is only a gif showing in the stage with some music(which is not delayed in loading, because it is an instance variable). I don't want to create many instance variables for each of the elements i wish to put into the stage. Below is the code, where only the necessary code is shown to understand the problem:
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
Dimension window = Toolkit.getDefaultToolkit().getScreenSize();
private double screenWidth = window.getWidth();
private double screenHeight = window.getHeight();
private boolean fullscreen = false;
private boolean music = false;
private double value = 0;
private Group root, root2;
private Image start;
private Image startHover;
private ImageView startViewer;
private Stage stage;
private boolean firstWindowExists = true;
private CheckBox full;
private CheckBox sound;
private Slider slider;
private Image BG;
private ImageView BGView;
private String path = "bin/Audio/8_bit_march.mp3";
private Media media;
private MediaPlayer player;
public static void main(String[] args) {
launch();
}
private void run() {
Image title = new Image("Title.png", 300, 0, true, true);
ImageView titleView= new ImageView();
titleView.setX(200);
titleView.setY(10);
titleView.setImage(title);
add(titleView);
start = new Image("Start1.png", 200, 0, true, true);
startHover = new Image("Start_Hover1.png", 200, 0, true, true);
startViewer= new ImageView();
startViewer.setX(250);
startViewer.setY(300);
startViewer.setOnMouseEntered(mouseEnter);
startViewer.setOnMouseExited(mouseExit);
startViewer.setOnMouseReleased(mouseReleased);
startViewer.setImage(start);
add(startViewer);
if (!firstWindowExists) {//this code works
Image title2 = new Image("BG.gif", (screenWidth/1.25) + 4, (screenHeight/1.25)+4, false, true);
ImageView titleView2= new ImageView();
titleView2.setX(-2);
titleView2.setY(-2);
titleView2.setImage(title2);
add2(titleView2);
}
}
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
if (sound.isSelected()) { // the checkbox is checked to be enabled
slider.setVisible(true);
}
else {
slider.setVisible(false);
}
}
};
EventHandler<MouseEvent> mouseEnter = new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) { // irrelevant for this problem
startViewer.setImage(startHover);
}
};
EventHandler<MouseEvent> mouseExit = new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
startViewer.setImage(start);
}
};
EventHandler<MouseEvent> mouseReleased = new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
stage.hide(); //hide first window
firstWindowExists = false;
Stage stage2 = new Stage(); // creating a new stage
root2 = new Group(); //creating new group
Scene scene2 = new Scene(root2, Color.WHITE);// creating a scene and adding the newly created Group
Canvas canvas2 = new Canvas(screenWidth/1.25, screenHeight/1.25); // creating a canvas for the screen
root2.getChildren().add(canvas2); // adding canvas to the group (window)
stage2.setTitle("Game"); // setting the title of the window
stage2.setScene(scene2); // Adds scene to the stage
stage2.setFullScreen(fullscreen);
stage2.show(); //after showing the screen here, it stays blank and then adds the title2 gif
stage2.centerOnScreen();
if (music) { //plays music
player.play();
player.setVolume(value);
player.setCycleCount(MediaPlayer.INDEFINITE);
}
run(); // runs the method with heavy lifting stuff
}
};
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), event -> {
}));
private void add(Node node) {
if (firstWindowExists) {
root.getChildren().add(node);
}
}
private void add2(Node node) {
root2.getChildren().add(node);
}
#Override
public void start(Stage stage) throws Exception { // main method which creates the first window
this.stage = stage;
stage.setTitle("Test GUI");
root = new Group();
Scene scene = new Scene(root, Color.DARKGRAY);
Canvas canvas = new Canvas(700, 350);
root.getChildren().add(canvas);
stage.setScene(scene);
stage.setResizable(false);
stage.show();
stage.centerOnScreen();
root.requestFocus();
run();
media = new Media(new File(path).toURI().toString());
player = new MediaPlayer(media);
}
}

JavaFX Custom Table Cell - Strange behavior

I have this code:
import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Callback;
public class Example extends Application
{
#Override
public void start(Stage stage) throws Exception
{
TableView<Color> table = new TableView<>();
ObservableList<Color> colors = FXCollections.observableArrayList();
table.setItems(colors);
table.setEditable(true);
TableColumn<Color, Color> column = new TableColumn<>();
column.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue()));
column.setCellFactory(param ->
{
ObservableList<Color> menuColors = FXCollections.observableArrayList();
menuColors.addAll(Color.RED, Color.GREEN, Color.BLUE);
return new ComboBoxTableCell(menuColors);
});
Button button = new Button("Add row");
button.setOnAction(event -> colors.add(Color.BLACK));
VBox box = new VBox(table, button);
table.getColumns().add(column);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
stage.setScene(new Scene(box));
stage.show();
}
public class ComboBoxTableCell extends TableCell<Color, Color>
{
private ComboBox<Color> comboBox;
public ComboBoxTableCell(ObservableList<Color> colors)
{
comboBox = createFancyComboBox(colors);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
#Override
public void updateItem(Color item, boolean empty)
{
if (item == null || empty)
setGraphic(null);
else
setGraphic(comboBox);
}
}
private ComboBox<Color> createFancyComboBox(ObservableList<Color> colors)
{
ComboBox<Color> comboBox = new ComboBox<>(colors);
Callback<ListView<Color>, ListCell<Color>> factory = list -> new ColorSquare();
comboBox.setCellFactory(factory);
comboBox.setButtonCell(factory.call(null));
comboBox.setValue(colors.get(0));
return comboBox;
}
public static class ColorSquare extends ListCell<Color>
{
#Override
public void updateItem(Color item, boolean empty)
{
super.updateItem(item, empty);
Rectangle rect = new Rectangle(18, 18);
if (item != null)
{
rect.setFill(item);
setGraphic(rect);
}
}
}
public static void main(String[] args)
{
launch(args);
}
}
If I try to run it, click on the button, change the color to for Green and click 8 times on the button, the green square will disapper.
How do I fix this and why is it happening? The real code isn't much different from this, this is the only problem I have. Thank you.
I have noticed that at times the rerendering of a table cell doesn't work right after a certain number of attempts. I had one where I was updating a remaining amount when the user entered a value in another cell.
To solve this I would toggle visibility of the column.
column.setVisible(false);
column.setVisible(true);
I would do this immediately after the action that changed the value in the cell.
It's a hack but seems to work.

FXML, JavaFX 8, TableView: Make a delete button in each row and delete the row accordingly

I am working on a TableView (FXML) where I want to have all the rows accompanied with a delete button at the last column.
Here's a video that shows what I mean: YouTube Delete Button in TableView
Here's what I have in my main controller class:
public Button del() {
Button del = new Button();
del.setText("X");
del.setPrefWidth(30);
del.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
int i = index.get();
if(i > -1) {
goals.remove(i);
list.getSelectionModel().clearSelection();
}
}
});
return del;
}
private SimpleIntegerProperty index = new SimpleIntegerProperty();
#Override
public void initialize(URL location, ResourceBundle resources){
//DateFormat df = new SimpleDateFormat("dd MMM yyyy");
sdate.setValue(LocalDate.now());
edate.setValue(LocalDate.now());
seq.setCellValueFactory(new PropertyValueFactory<Goals, Integer>("id"));
gol.setCellValueFactory(new PropertyValueFactory<Goals, String>("goal"));
sdt.setCellValueFactory(new PropertyValueFactory<Goals, Date>("sdte"));
edt.setCellValueFactory(new PropertyValueFactory<Goals, Date>("edte"));
prog.setCellValueFactory(new PropertyValueFactory<Goals, Integer>("pb"));
del.setCellValueFactory(new PropertyValueFactory<Goals, Button>("x"));
list.setItems(goals);
list.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
#Override
public void changed(ObservableValue<?> observable,
Object oldValue, Object newValue) {
index.set(goals.indexOf(newValue));
System.out.println("Index is: "+goals.indexOf(newValue));
}
});
}
Each time I launch the application, I will try to click the delete button from random rows but it always delete the first row. I guess the addListener method I use for list is not properly implemented and indexOf(newValue) is always 0 at every initialisation.
However, it will work if I click a row first and then click the delete button. But this is not what I want. I want users to be able to delete any row if they press the delete button without selecting the row.
Appreciate your help guys!
You need a custom cell factory defined for the column containing the delete button.
TableColumn<Person, Person> unfriendCol = new TableColumn<>("Anti-social");
unfriendCol.setCellValueFactory(
param -> new ReadOnlyObjectWrapper<>(param.getValue())
);
unfriendCol.setCellFactory(param -> new TableCell<Person, Person>() {
private final Button deleteButton = new Button("Unfriend");
#Override
protected void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
if (person == null) {
setGraphic(null);
return;
}
setGraphic(deleteButton);
deleteButton.setOnAction(
event -> getTableView().getItems().remove(person)
);
}
});
Here is a sample app. It doesn't use FXML, but you could adapt it to work with FXML very easily. Just click on an "Unfriend" button in the "Anti-social" column to delete a friend. Do it a lot and you will soon run out of friends.
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class GestureEvents extends Application {
private TableView<Person> table = new TableView<>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith"),
new Person("Isabella", "Johnson"),
new Person("Ethan", "Williams"),
new Person("Emma", "Jones"),
new Person("Michael", "Brown")
);
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
final Label label = new Label("Friends");
label.setFont(new Font("Arial", 20));
final Label actionTaken = new Label();
TableColumn<Person, Person> unfriendCol = new TableColumn<>("Anti-social");
unfriendCol.setMinWidth(40);
unfriendCol.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue()));
unfriendCol.setCellFactory(param -> new TableCell<Person, Person>() {
private final Button deleteButton = new Button("Unfriend");
#Override
protected void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
if (person == null) {
setGraphic(null);
return;
}
setGraphic(deleteButton);
deleteButton.setOnAction(event -> data.remove(person));
}
});
TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<>("firstName"));
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<>("lastName"));
table.setItems(data);
table.getColumns().addAll(unfriendCol, firstNameCol, lastNameCol);
table.setPrefHeight(250);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 10, 10, 10));
vbox.getChildren().addAll(label, table, actionTaken);
VBox.setVgrow(table, Priority.ALWAYS);
stage.setScene(new Scene(vbox));
stage.show();
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private Person(String fName, String lName) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
}
}

How to fire event when scrolling up,javafx

I am developing chat application on java and i want to use function used by facebook to retrieve chat history when user scroll up. I tried "on Scroll" action which fire event whenever scroll reach top or down of scroll bar. Now i want to Fire action event only when scroll bar reach top like in facbook chat box.
Assuming that you have a ScrollPane, you can observe vvalueProperty of it:
scrollPane.vvalueProperty().addListener(
(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
if(newValue.doubleValue() == 0){
System.out.println( "AT TOP" );
// load more items
}
});
and you may also scroll to bottom with
scrollPane.setVvalue( scrollPane.getVmax() );
initially.
Here is the example of what you want. I hope this will help you.
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.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ScrollUtil extends Application
{
private boolean scrollToBottom = false;
private boolean scrollToTop = false;
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(final Stage stage) throws Exception
{
final VBox root = new VBox();
final ScrollPane scrollPane = new ScrollPane();
final VBox vBox = new VBox();
vBox.setAlignment(Pos.BOTTOM_CENTER);
scrollPane.setContent(vBox);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
Button button = new Button("add");
Button button3 = new Button("scroll up");
button3.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent actionEvent)
{
scrollToTop = true;
scrollPane.setVvalue(scrollPane.getVmin());
System.out.println("vmin= "+scrollPane.getVmin() + "; vmax = " + scrollPane.getVmax() + "; vvalue" + scrollPane.getVvalue());
}
});
button.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent actionEvent)
{
vBox.getChildren().add(new Label("hallo"));
scrollToBottom = true;
//System.out.println(scrollPane.getVmin() + "; max = " + scrollPane.getVmax() + "; " + scrollPane.getVvalue());
}
});
scrollPane.setVvalue(scrollPane.getVmax());
scrollPane.vvalueProperty().addListener(new ChangeListener<Number>()
{
#Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
{
if(scrollToBottom)
{
scrollPane.setVvalue(scrollPane.getVmax());
scrollToBottom = false;
}
if(newValue.doubleValue() ==0)
{
vBox.getChildren().add(new Label("hallo"));
System.out.println("min value ="+scrollPane.getVmin());
//scrollPane.setVvalue(scrollPane.getVmin());
scrollToTop = true;
System.out.println("now get the chat history");
}
}
});
Button button2 = new Button("scroll");
button2.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent actionEvent)
{
scrollPane.setVvalue(Double.MAX_VALUE);
}
});
root.getChildren().addAll(scrollPane, button, button2, button3);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}

Is there a way to bind the content of a ListProperty in JavaFX?

Is there an existing way to bind the content of a ListProperty? Consider the following:
private final ListProperty<Worker<?>> workers = new SimpleListProperty<>(FXCollections.observableArrayList());
public ListProperty<Worker<?>> workersProperty() {return workers;}
public ObservableList<Worker<?>> getWorkers() {return workers.get();}
public void setWorkers(ObservableList<Worker<?>> workers) {this.workers.set(workers);}
private final ObservableList<Worker<?>> stateWatchedWorkers = FXCollections.observableArrayList(
new Callback<Worker<?>, Observable[]>() {
#Override
public Observable[] call(final Worker<?> param) {
return new Observable[]{
param.stateProperty()
};
}
}
);
What I want to do is bind the content of the stateWatchedWorkers list to workers. If the entire workers list is replaced, I want stateWatchedWorkers to be updated to match the content of the new list. Put another way, I want the stateWatchedWorkers list to be mapped to the current list being held by the workers property.
You can do
Bindings.bindContent(stateWatchedWorkers, workers);
(since ListProperty<T> implements ObservableList<T>, by delegating the ObservableList methods to the wrapped list, etc).
Here's a complete example:
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ListContentBindingExample extends Application {
private static final Random RNG = new Random();
private static final ExecutorService exec = Executors.newFixedThreadPool(5, r -> {
Thread t = new Thread(r);
t.setDaemon(true);
return t ;
});
#Override
public void start(Stage primaryStage) {
final ListProperty<Worker<?>> workers = new SimpleListProperty<>(FXCollections.observableArrayList());
final ObservableList<Worker<?>> stateWatchedWorkers = FXCollections.observableArrayList(worker -> new Observable[] { worker.stateProperty() });
Bindings.bindContent(stateWatchedWorkers, workers);
Button newWorker = new Button("New Worker") ;
newWorker.setOnAction(event -> workers.add(createTask()));
Button replaceAllWorkers = new Button("New Worker List");
replaceAllWorkers.setOnAction(event -> {
ObservableList<Worker<?>> newWorkers = FXCollections.observableArrayList();
for (int i=0; i<=10; i++) {
newWorkers.add(createTask());
}
workers.set(newWorkers);
});
ListView<Worker<?>> workerView = new ListView<>();
workerView.setCellFactory(listView -> new ListCell<Worker<?>>() {
#Override
public void updateItem(Worker<?> worker, boolean empty) {
super.updateItem(worker, empty);
if (empty) {
setText(null);
} else {
setText(worker.getState().toString());
}
}
});
workerView.setItems(stateWatchedWorkers);
workers.get().addListener((Change<? extends Worker<?>> change) -> stateWatchedWorkers.forEach(w -> System.out.println(w.getState())));
stateWatchedWorkers.addListener((Change<? extends Worker<?>> change) -> stateWatchedWorkers.forEach(w -> System.out.println(w.getState())));
HBox buttons = new HBox(5, newWorker, replaceAllWorkers);
buttons.setAlignment(Pos.CENTER);
buttons.setPadding(new Insets(10));
primaryStage.setScene(new Scene(new BorderPane(workerView, null, null, buttons, null), 250, 400));
primaryStage.show();
}
private Worker<Void> createTask() {
Service<Void> service = new Service<Void>() {
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
public Void call() throws Exception {
Thread.sleep(1000 + RNG.nextInt(2000));
return null ;
}
};
}
};
service.setExecutor(exec);
service.start();
return service ;
}
public static void main(String[] args) {
launch(args);
}
}
James_D's hint about ListProperty delegating to its ObservableList is the correct answer. Here's another code example that shows binding the content of ListProperty "just works".
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.util.Callback;
public class ListPropertyContentBinding {
/* This is a quick test to see how ListProperty notifies about list changes
* when the entire list is swapped.
*
* Conclusion: When the backing list is changed, ListProperty will perform
* notification indicating that all items from the original list were
* removed and that all items from the updated (new) list were added.
*/
ObservableList<Person> oldPeople = FXCollections.observableArrayList();
ObservableList<Person> newPeople = FXCollections.observableArrayList();
// A list property that is used to hold and swap the lists.
ListProperty<Person> people = new SimpleListProperty<>(oldPeople);
// A list that includes an extractor. This list will be bound to people and
// is expected to additionally notify whenever the name of a person in the
// list changes.
ObservableList<Person> nameWatchedPeople = FXCollections.observableArrayList(
new Callback<Person, Observable[]>() {
#Override
public Observable[] call(final Person person) {
return new Observable[]{
person.name
};
}
});
public ListPropertyContentBinding() {
Bindings.bindContent(nameWatchedPeople, people);
nameWatchedPeople.addListener((ListChangeListener<Person>) change -> {
while (change.next()) {
System.out.println(" " + change.toString());
}
});
Person john = new Person("john");
System.out.println("Adding john to oldPeople. Expect 1 change.");
oldPeople.add(john);
System.out.println("Changing john's name to joe. Expect 1 change.");
john.name.set("joe");
Person jane = new Person("jane");
System.out.println("Adding jane to newPeople. Expect 0 changes.");
newPeople.add(jane);
System.out.println("Updating people to newPeople. Expect ? changes.");
people.set(newPeople);
Person jacob = new Person("jacob");
System.out.println("Adding jacob to oldPeople. Expect 0 changes.");
oldPeople.add(jacob);
System.out.println("Adding jacob to newPeople. Expect 1 change.");
newPeople.add(jacob);
System.out.println("Changing jacob's name to jack. Expect 1 change.");
jacob.name.set("jack");
System.out.println("Updating people to oldPeople. Expect ? changes.");
System.out.println(" oldPeople: " + oldPeople.toString());
System.out.println(" newPeople : " + newPeople.toString());
people.set(oldPeople);
}
public static void main(String[] args) {
new ListPropertyContentBinding();
}
class Person {
private final StringProperty name = new SimpleStringProperty();
public Person(String name) {
this.name.set(name);
}
#Override
public String toString() {
return name.get();
}
}
}
And here's the output from the above example:
Adding john to oldPeople. Expect 1 change.
{ [john] added at 0 }
Changing john's name to joe. Expect 1 change.
{ updated at range [0, 1) }
Adding jane to newPeople. Expect 0 changes.
Updating people to newPeople. Expect ? changes.
{ [joe] removed at 0 }
{ [jane] added at 0 }
Adding jacob to oldPeople. Expect 0 changes.
Adding jacob to newPeople. Expect 1 change.
{ [jacob] added at 1 }
Changing jacob's name to jack. Expect 1 change.
{ updated at range [1, 2) }
Updating people to oldPeople. Expect ? changes.
oldPeople: [joe, jack]
newPeople : [jane, jack]
{ [jane, jack] removed at 0 }
{ [joe, jack] added at 0 }