如何使JavaFX列表视图中的前两行不可选 [英] How to make the first 2 rows in a JavaFX listview non-selectable

查看:198
本文介绍了如何使JavaFX列表视图中的前两行不可选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 playList.getSelectionModel()。select(1) - 突出显示的选项将是播放列表的第二行。如果我使用 playList.getSelectionModel()。select(-1)选择无行(如StackOverflow上其他地方所建议的那样)第一行将被选中。有谁知道为什么这不起作用?我希望listview的前两行永远不会有资格进行选择。

If I use playList.getSelectionModel().select(1) - the highlighted selection will be the second row of the playlist. If I use playList.getSelectionModel().select(-1) to have no rows selected (as suggested elsewhere on StackOverflow) the first row will be selected. Does anyone have any idea of why this is not working? I would like for the first two rows of the listview to never be eligible for selection.

我使用的是JavaFX-8。

I am using JavaFX-8.

public class AudioPlayerFXMLController implements Initializable {

@FXML
private ListView playList;
private static ObservableList<Object> playListItems;
private static final String NEW_PLAYLIST = "New PlayList";
private static final String FRIEND_PLAYLIST = "Friend's PlayList";

@Override
public void initialize(URL url, ResourceBundle rb) {

    playListItems = FXCollections.observableArrayList();
    playListItems.add(NEW_PLAYLIST);
    playListItems.add(FRIEND_PLAYLIST);

    playList.setItems(FXCollections.observableList(playListItems));


    playList.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
        @Override
        public ListCell<String> call(ListView<String> list) {
            return new ImageCell();
        }
    });

    playList.getSelectionModel().select(-1);

}

static class ImageCell extends ListCell<String> {

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if ((this.getIndex() == 0) || (this.getIndex() == 1)) {
            ImageView addSymbol;
            addSymbol = ImageViewBuilder.create().image(new Image("/images/ic_add_grey600_15dp.png")).build();
            addSymbol.fitHeightProperty();
            setText(item);
            setGraphic(addSymbol);

        } else {
            setText(null);
            setGraphic(null);
        }
    }

}

}

推荐答案

根据您使用的jdk8的确切(更新)版本,这可能是 RT-25679 (部分还原于 RT-38517 )AFAIK,没有公共API可以禁用集合视图的自动对焦/选择。只有ListView在其属性中禁用默认行为(未记录!注意,它们可以更改,恕不另行通知)条目

Depending on which exact (update) version of jdk8 you are using, this might be the fix for RT-25679 (partially reverted in RT-38517) AFAIK, there is no public api to disable the auto-focus/select for the collection views in general. Only ListView has (undocumented! beware, they can change without notice) entries in its properties that disable the default behaviour

// disable selecting the first item on focus gain - this is
// not what is expected in the ComboBox control (unlike the
// ListView control, which does this).
listView.getProperties().put("selectOnFocusGain", false);
// introduced between 8u20 and 8u40b7
// with this, testfailures back to normal
listView.getProperties().put("selectFirstRowByDefault", false);

这篇关于如何使JavaFX列表视图中的前两行不可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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