I am writing a mock hotel reservation system with two menus, employee and guest. Created rooms are stored in a master array called roomArray and added to a list view in the empl
Since you are using JavaFX, you should use ObservableList
for your rooms. Additionally, you would need to use FilteredList and FXCollections.observableArrayList(extractor).
This is how you can implement it:
public class Room {
public enum State {AVAILABLE, BOOKED}
private final ObjectProperty state = new SimpleObjectProperty<>(AVAILABLE);
public final ObjectProperty stateProperty() { return state; }
public final State getState() { return state.get(); }
public final void setState(final State value) { state.set(state); }
}
Main class:
private final ObservableList rooms;
public final ObservableList getRooms() { return rooms; }
private final ObservableList guestRooms;
public final ObservableList getGuestRooms() { return guestRooms; }
// Constructor
public MyClass() {
rooms = FXCollections.observableArrayList(room -> new Observable[] {room.stateProperty()});
guestRooms = rooms.filtered(room -> room.getState() == Room.State.AVAILABLE);
}
The guestRooms
list is just a wrapper for rooms
list, filtered with a Predicate
. The filtered list will change according to the rooms
list, but it will only react to changes to the list itself (add, remove, replace of elements). To make sure it responds to changes of the state of existing rooms, you need to use FXCollections.observableArrayList(extractor)
overload. This overload allows you to control which property in each Room
object would also trigger a ListChangeListener.Change
, which would also causes the filtered list to update itself.