JavaFX 引用了一个主数组列表 [英] JavaFX reference a master array list

查看:21
本文介绍了JavaFX 引用了一个主数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个包含员工和客人两个菜单的模拟酒店预订系统.创建的房间存储在名为 roomArray 的主数组中,并添加到员工菜单中的列表视图中,并添加到访客菜单中的列表视图中.可以使用或预订房间,但是客人菜单列表视图中只显示可用房间,所以我可能有 5 个房间,但客人菜单列表视图中只显示 2 个.如果用户点击第二个,我不想尝试在主 roomArray 静态 ArrayList 中预订索引 1 的房间,因为它们不匹配.

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 employee menu, and added to a list view in the guest menu. Rooms can be available or booked, however only available rooms are shown in the guest menu list view, so I might have 5 rooms but only 2 show in the guest menu list view. If the user clicks on the second one, I don't want to try and book the index 1 room in the main roomArray static ArrayList because they don’t match up.

例如,假设在员工列表视图中我有三个房间,其中两个已预订.在客人列表视图中,仅显示可用房间.因此,右侧的列表视图将显示所选索引为 0,但对于同一房间,主 roomArray 中的相同索引为 1.如何制作可参考主列表中房间的可用房间数组列表的中介?

For example, say in the employee list view I have three rooms, two of which are booked. In the guest list view, only the available rooms show up. So the list view on the right would show a selected index of 0, but the same index in the master roomArray is 1 for that same room. How can I make an intermediary of array list of available rooms that reference rooms in the master list?

内森

推荐答案

由于您使用的是 JavaFX,因此您的房间应该使用 ObservableList.此外,您还需要使用 FilteredListFXCollections.observableArrayList(extractor).

Since you are using JavaFX, you should use ObservableList for your rooms. Additionally, you would need to use FilteredList and FXCollections.observableArrayList(extractor).

您可以这样实现它:

public class Room {
    public enum State {AVAILABLE, BOOKED}

    private final ObjectProperty<State> state = new SimpleObjectProperty<>(AVAILABLE);
    public final ObjectProperty<State> stateProperty() { return state; }
    public final State getState() { return state.get(); }
    public final void setState(final State value) { state.set(state); }
}

主类:

private final ObservableList<Room> rooms;
public final ObservableList<Room> getRooms() { return rooms; }

private final ObservableList<Room> guestRooms;
public final ObservableList<Room> getGuestRooms() { return guestRooms; }

// Constructor
public MyClass() {
    rooms = FXCollections.observableArrayList(room -> new Observable[] {room.stateProperty()});

    guestRooms = rooms.filtered(room -> room.getState() == Room.State.AVAILABLE);
}

guestRooms 列表只是rooms 列表的包装器,用Predicate 过滤.过滤后的列表会根据 rooms 列表而变化,但它只会对列表本身的变化做出反应(添加、删除、替换元素).为了确保它响应现有房间状态的变化,您需要使用 FXCollections.observableArrayList(extractor) 重载.此重载允许您控制每个 Room 对象中的哪个属性也会触发 ListChangeListener.Change,这也会导致过滤列表自行更新.

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.

这篇关于JavaFX 引用了一个主数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆