简体   繁体   English

将JavaFx ListView的选择索引绑定到整数属性

[英]Binding a JavaFx ListView's selection index to an integer property

Is it possible to bind (one-way) a ListView's selection index or item to a property? 是否可以将ListView的选择索引或项目绑定(单向)到属性?

I can get a ReadOnlyIntegerProperty with this call, but it's a ReadOnlyProperty which does not have the bind methods you see in ObjectProperty , StringProperty , etc. 我可以通过此调用获得ReadOnlyIntegerProperty,但这是一个ReadOnlyProperty,它没有在ObjectPropertyStringProperty等中看到的绑定方法。

myListView.getSelectionModel().selectedIndexProperty()

How do I go about binding an Integer Property to my ListView's selected index property? 如何将Integer属性绑定到ListView的选定索引属性?

The API exposes the selectedIndex as a ReadOnlyProperty because it doesn't want to expose the bind method that is part of the Property API. API不想将selectedIndex公开为ReadOnlyProperty因为它不想公开属于Property API的bind方法。 The reason it doesn't want to expose the bind method is that the ListView needs to be able to set the property when the user clicks on it. 不想公开bind方法的原因是,当用户单击ListView ,它必须能够设置该属性。 If the bind method were part of the API, and the programmer called listView.getSelectionModel().selectedIndexProperty().bind(...) , then runtime errors would be generated whenever the user clicked on the list. 如果bind方法是API的一部分,并且程序员调用listView.getSelectionModel().selectedIndexProperty().bind(...) ,则每当用户单击列表时,都会生成运行时错误。

Because the bindBidirectional is also part of the Property API, but not the ReadOnlyProperty API, this means that you also cannot bidirectionally bind the selectedIndex . 因为bindBidirectional也是Property API的一部分,但不是ReadOnlyProperty API的一部分,所以这意味着您也不能双向绑定selectedIndex

You can achieve the same effect as bidirectional binding using two listeners: 您可以使用两个侦听器实现与双向绑定相同的效果:

IntegerProperty property = new SimpleIntegerProperty();
ListView<?> listView = new ListView<?>();

// change property when selection changes:
listView.getSelectionModel().selectedIndexProperty().addListener((obs, oldIndex, newIndex) ->
    property.set(newIndex.intValue()));

// change selection when property changes:
property.addListener((obs, oldValue, newValue) -> 
    listView.getSelectionModel().clearAndSelect(newValue.intValue())); 

If you just want to bind the selected item index to the property, you only need the second listener. 如果仅要将所选项目索引绑定到属性,则仅需要第二个侦听器。

If you just want to bind the property to the selected item index, and you are not going to set the property by other means, then of course you can just bind the property: 如果您只想将属性绑定到选定的项目索引,而又不想通过其他方式设置属性,那么当然可以将属性绑定:

IntegerProperty property = new SimpleIntegerProperty();
property.bind(listView.getSelectionModel().selectedIndexProperty());

(Though in this case, you could always just reference the selectedIndexProperty directly if your design allows.) (尽管在这种情况下,如果设计允许,您始终可以直接直接引用selectedIndexProperty 。)

I can understnad not permitting biDirectionalBinding since it's read only, but how can i do one way binding? 我可以理解biDirectionalBinding因为它是只读的,所以我该怎么做?

Try: 尝试:

IntegerProperty property = new SimpleIntegerProperty();
property.bind(myListView.getSelectionModel().selectedIndexProperty());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM