JavaFX-在ListView中创建不可选项
我正在使用调用的库中的JavaFX-在ListView中创建不可选项,java,javafx,Java,Javafx,我正在使用调用的库中的JFXListView和JFXListCell,其用途和功能与常规的ListView相同 该列表包含一些标签、按钮和锚烷。在列表的顶部和底部,我想添加不可选择的项。鼠标单击时不应选择项目,不应能够聚焦,不应能够滚动 我想到了使用updateItem()函数并设置项disable: @FXML JFXListView listView; ObservableList<AnchorPane> list = FXCollections.observableArrayL
JFXListView
和JFXListCell
,其用途和功能与常规的ListView
相同
该列表包含一些标签
、按钮
和锚烷
。在列表的顶部和底部,我想添加不可选择的项。鼠标单击时不应选择项目,不应能够聚焦
,不应能够滚动
我想到了使用updateItem()
函数并设置项disable:
@FXML
JFXListView listView;
ObservableList<AnchorPane> list = FXCollections.observableArrayList();
private void initializeListView(){
AnchorPane headerBottomPane = new AnchorPane();
headerBottomPane.setId("headerBottomPane");
....//some property of AnchorPane
list.add(headerBottomPane); //Add header AnchorPane
while(true){
AnchorPane listContainer = new AnchorPane();
Label title = new Label();
Label subtitle = new Label();
Button button = new Button();
Button button2 = new Button();
//Some code here...
listContainer.getChildren().addAll(label, subtitle, button, button2);
list.add(listContainer);
//some code here...
}
list.add(headerBottomPane); //Add bottom AnchorPane
listView.setCellFactory(new CallBack<JFXListView, JFXListCell>(){
@Override
public JFXListCell call(JFXListView param){
return new JFXListCell<AnchorPane>(){
@Override
protected void updateItem(AnchorPane anchorPane, boolean empty){
super.updateItem(anchorPane, empty);
if(anchorPane != null){
if(anchorPane.getId.equals("headerBottomPane")){
setDisable(true);
}
setItem(anchorPane);
}else{
setItem(null);
}
}
};
}
});
}
@FXML
JFXListView listView;
ObservableList=FXCollections.observableArrayList();
私有void initializeListView(){
锚烷头锚烷=新锚烷();
headerBottomPane.setId(“headerBottomPane”);
..//锚烷的某些性质
list.add(headerBottomPane);//添加header锚烷
while(true){
AnchorPane listContainer=新的AnchorPane();
标签标题=新标签();
标签字幕=新标签();
按钮按钮=新按钮();
Button button2=新按钮();
//这里有一些代码。。。
listContainer.getChildren().addAll(标签、字幕、按钮、按钮2);
list.add(listContainer);
//这里有一些代码。。。
}
list.add(headerBottomPane);//添加底部锚烷
setCellFactory(新回调(){
@凌驾
公共JFXListCell调用(JFXListView参数){
返回新的JFXListCell(){
@凌驾
受保护的void updateItem(AnchorPane AnchorPane,布尔空){
super.updateItem(anchorPane,空);
if(anchorPane!=null){
if(anchorPane.getId.equals(“headerBottomPane”)){
设置禁用(true);
}
设置项(锚烷);
}否则{
setItem(null);
}
}
};
}
});
}
我可以禁用列表的顶部和最后一个项目,该项目无法再使用鼠标单击进行选择
但问题是,当我使用键盘向上箭头
和向下箭头
时,它是可聚焦的。另一件奇怪的事情是,当我使用鼠标滚轮
滚动列表时,一些项目也变得不可选择。我会考虑只使用VBox,然后将顶部不可选择的项目放在第一位,然后是包含所有可选择项的ListView,然后是底部的不可选择项…我认为您必须将ListView.setCellFactory()
函数放在添加这些项的代码顶部,在添加项之前尝试初始化它
在updateItem()
中尝试使用setMouseTransparent()
和setFocusTravesable()
我没有测试它,但我希望它能工作。我想你正在寻找SelectionModel
和FocusModel
(如果JFXListVies
使用了这些,当然,我不熟悉jfoenix),为什么要在列表中添加控件?这几乎总是错的:列表项应该是由控件可视化的数据对象(在listCell中可重复使用)@kleopatra-我认为这没关系,列表也可以有节点。@Polar当然可以,但当有人向列表中添加控件时,大约99%她试图用错误的方法解决真正的问题;)这是试图帮助控制,但无助于真正的控制problem@kleopatra-我明白了。。。谢谢你的知识,我不知道。LOL:)
@Override
protected void updateItem(AnchorPane anchorPane, boolean empty){
super.updateItem(anchorPane, empty);
if(anchorPane != null){
if(anchorPane.getId.equals("headerBottomPane")){
setItem(anchorPane); //moved at the top
setMouseTransparent(true); //added this line
setFocusTraversable(false); //added this line
setDisable(true);
}else{
setItem(null);
}
}