Reputation: 33
I have an object of type "MyClass" called "myClassInstance" which has an ArrayList of objects. All contained objects implement the Serializable interface and will be written to file.
I need to be able to know when this object has changed in any way to update a JavaFX ListView whenever there are any changes to the object (Including any objects it contains).
Essentially any time "myClassInstance.hashCode" would return a different number, I want to be able to call an update method to update ui elements.
I have looked for a solution to this problem and found many pages which seem to have solutions, however I cant help but think these solutions which all seem to require a significant rewrite of the model classes I have all are entirely too complex for the problem I have.
Almost all seem to refer to Property Change Listeners, to know about changes to specific Properties/fields, but I do not need anything this specific. I simply need to know whether or not the object "myClassInstance" has been changed. Has the value returned by "myClassInstance.hashCode" changed. Not if any of the properties have changed, or if the properties of the objects it contains changed but if anything in "myClassInstance" has changed.
Class that needs to be observed:
public class myClass implements Serializable {
private ArrayList<SpecificObjectType> myObjects = new ArrayList<>();
public myClass () {
newSpecificObjectType(); //creates default specific object type
}
// Classes related to changing, retrieving or deleting elements of myObjects
}
FXMLMainDocument.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" "package.FXMLMainController">
<center>
<ListView fx:id="listViewObjects" BorderPane.alignment="CENTER" />
</center>
<top>
<Button fx:id="editButton" onAction="#handleEditAction" mnemonicParsing="false" text="Edit" BorderPane.alignment="CENTER" />
</top>
</BorderPane>
FXMLEditDocument.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="package.FXMLEditController">
<center>
<TextField fx:id="edit" onAction="#handleNameTextField" promptText="This Edits The Name of One of the Objects in MyClass's ArrayList" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
FXMLMainController:
@FXML
private ListView listViewObjects;
private MyClass myClassInstance;
private ObservableList<String> obsObjects;
...
public void update() {
this.obsObjects.setAll(myClassInstance.getMyObjectNames());
}
@Override
public void initialize(URL url, ResourceBundle rb) {
this.myClassInstance = new MyClass();
obsObjects = FXCollections.observableArrayList(myClassInstance.getMyObjectNames());
this.listViewObjects.setItems(obsObjects);
update();
}
I have decided to try the Observable, Observer route. The secondary controller is now observable and the primary an observer. When certain actions are triggered in the secondary, the primary updates. This works, although Im not fully certain it can do everything I want it to. Il probably have a sit on this after which I'll Label the relevant answer.
Upvotes: 0
Views: 1272
Reputation: 10253
Your class doesn't appear to have any public
fields, correct? Could you not simply add a BooleanProperty
to the class and update it from any setters in the class?
For instance:
private BooleanProperty hasChanged = new SimpleBooleanProperty();
Then in any of your setters, update that property:
public void setData(String data) {
this.data = data;
this.hasChanged.set(true);
}
Upvotes: 1
Reputation: 2213
You can use Java's Observables
. The object you want to monitor should extend Observable
, and the monitoring class should implement Observer
.
See the documentation for Observable
and Observer
.
Upvotes: 1