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

查看:93
本文介绍了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?

Nathan

推荐答案

由于您使用的是JavaFX,因此您应该为您的房间使用 ObservableList 。此外,您需要使用 FilteredList FXCollections.observableArrayList(提取器)

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(提取器)重载。此重载允许您控制每个 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天全站免登陆