Assume I have such simple domain POJO class:
package sample;
public class Item {
private String name;
private String state;
public Item() {}
public Item(String name, String state) {
this.name = name;
this.state = state;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
And assume that I have one restriction: I am not allowed to make any changes to this class. So I need to write a wrapper, right?
package sample;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
public class ItemWrapper {
private ObjectProperty<Item> itemProperty = new SimpleObjectProperty<>();
private Item item;
public ItemWrapper(Item item) {
setItem(item);
}
public Item getItem() {
return itemProperty.get();
}
public void setItem(Item item) {
this.item = item;
itemProperty.set(item);
}
public ObjectProperty<Item> itemProperty(){
return itemProperty;
}
}
Then assume that I have a little JavaFX app which can show a list of items and has possibility to change item's state. Let's look at it.
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Item View");
primaryStage.setScene(new Scene(root, 200, 100));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="108.0" prefWidth="225.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label layoutX="14.0" layoutY="14.0" text="show item" />
<ComboBox fx:id="cbxItemsSelector" layoutX="14.0" layoutY="31.0" prefWidth="150.0" />
<Button fx:id="btnChangeState" layoutX="14.0" layoutY="65.0" mnemonicParsing="false" onAction="#changeItemState" prefHeight="25.0" prefWidth="85.0" text="change state" />
</children>
</AnchorPane>
Controller.java
package sample;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Controller {
@FXML
private ComboBox<ItemWrapper> cbxItemsSelector;
private ObservableList<ItemWrapper> items;
public void initialize(){
loadItems();
customizeSelectorCellView();
}
private void loadItems() {
List<Item> itemsList = new ArrayList<>();
itemsList.add(new Item("first item","initialized"));
itemsList.add(new Item("second item","initialized"));
List<ItemWrapper> itemWrappers = itemsList.stream().map(ItemWrapper::new).collect(Collectors.toList());
items = FXCollections.observableList(itemWrappers,wrapper -> new Observable[]{wrapper.itemProperty()});
cbxItemsSelector.setItems(items);
}
private void customizeSelectorCellView(){
cbxItemsSelector.setButtonCell(new ListCell<ItemWrapper>() {
@Override
protected void updateItem(ItemWrapper itemWrapper, boolean empty) {
super.updateItem(itemWrapper, empty);
if (empty) {
setText("");
} else {
setText(itemWrapper.getItem().getName());
}
}
});
cbxItemsSelector.setCellFactory(
new Callback<ListView<ItemWrapper>, ListCell<ItemWrapper>>() {
@Override
public ListCell<ItemWrapper> call(ListView<ItemWrapper> p) {
ListCell cell = new ListCell<ItemWrapper>() {
@Override
protected void updateItem(ItemWrapper itemWrapper, boolean empty) {
//System.out.println("update item");
super.updateItem(itemWrapper, empty);
if (empty) {
setText("");
} else {
Item item = itemWrapper.getItem();
setText(String.format("name: %s\n state: %s\n",item.getName(),item.getState()));
}
}
};return cell;
}
}
);
}
@FXML
public void changeItemState(){
ItemWrapper selectedItemWrapper = cbxItemsSelector.getSelectionModel().getSelectedItem();
if (selectedItemWrapper == null) return;
Item item = selectedItemWrapper.getItem();
item.setState("started");
}
}
The problem is that the comboBox does not see changes applied after some of item's state updated. The reason of the problem is that I only change object's (i.e Item) fields and I don't set new Item via setItem
method.
So it seems that the fields changes are not observable.
I also know that IF I replace SimpleObjectProperty
to two StringProperty
(for name and state fields) in ItemWrapper
class it will work. But I don't want to make field duplication. What If I would have 100 fields in Item
class?
And I have two questions:
1) How can I make ComboBox see changes made by some other external side in any field in Item
class without field duplication (without creating additional field properties) ? Maybe should I use some special binding?
2) Is there a way to get rid of Wrapper class while stay domain Item
class unmodified?