如何使JavaFX listview中的前2行不可选择
如果我使用播放列表.getSelectionModel().select(1)-突出显示的选择将是播放列表的第二行。如果我使用playList.getSelectionModel().select(-1)选择无行(如StackOverflow上的其他地方所建议的),将选择第一行。有人知道为什么这不起作用吗?我希望listview的前两行永远不能被选择 我正在使用JavaFX-8如何使JavaFX listview中的前2行不可选择,java,javafx,javafx-8,Java,Javafx,Javafx 8,如果我使用播放列表.getSelectionModel().select(1)-突出显示的选择将是播放列表的第二行。如果我使用playList.getSelectionModel().select(-1)选择无行(如StackOverflow上的其他地方所建议的),将选择第一行。有人知道为什么这不起作用吗?我希望listview的前两行永远不能被选择 我正在使用JavaFX-8 public class AudioPlayerFXMLController implements Initializ
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);
}
}
}
公共类AudioPlayerXmlController实现可初始化{
@FXML
私有列表视图播放列表;
私有静态可观察列表项;
私有静态最终字符串NEW\u PLAYLIST=“NEW PLAYLIST”;
私有静态最终字符串FRIEND\u PLAYLIST=“FRIEND's PLAYLIST”;
@凌驾
公共void初始化(URL、ResourceBundle rb){
playlitems=FXCollections.observearraylist();
playlitems.add(新建播放列表);
播放项目。添加(朋友\播放列表);
playList.setItems(FXCollections.observableList(playListItems));
playList.setCellFactory(新回调(){
@凌驾
公共ListCell调用(ListView列表){
返回新的ImageCell();
}
});
playList.getSelectionModel().select(-1);
}
静态类ImageCell扩展了ListCell{
@凌驾
public void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
if((this.getIndex()=0)| |(this.getIndex()=1)){
图像视图添加符号;
addSymbol=ImageViewBuilder.create().image(新图像(“/images/ic_add_grey600_15dp.png”)).build();
addSymbol.fitHeightProperty();
setText(项目);
设置图形(addSymbol);
}否则{
setText(空);
设置图形(空);
}
}
}
}根据您使用的jdk8的确切(更新)版本,这可能是AFAIK的修复(在中部分恢复),通常没有公共api来禁用集合视图的自动聚焦/选择。只有ListView的属性中有(未记录!注意,它们可以在不通知的情况下更改)项禁用默认行为
// 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);
根据您使用的jdk8的确切(更新)版本,这可能是AFAIK(在中部分恢复)的修复程序,通常没有公共api来禁用集合视图的自动聚焦/选择。只有ListView的属性中有(未记录!注意,它们可以在不通知的情况下更改)项禁用默认行为
// 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);
根据您使用的jdk8的确切(更新)版本,这可能是AFAIK(在中部分恢复)的修复程序,通常没有公共api来禁用集合视图的自动聚焦/选择。只有ListView的属性中有(未记录!注意,它们可以在不通知的情况下更改)项禁用默认行为
// 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);
根据您使用的jdk8的确切(更新)版本,这可能是AFAIK(在中部分恢复)的修复程序,通常没有公共api来禁用集合视图的自动聚焦/选择。只有ListView的属性中有(未记录!注意,它们可以在不通知的情况下更改)项禁用默认行为
// 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);
我想我可能会改变这两行的选择颜色以匹配它们的背景。我想我可能会改变这两行的选择颜色以匹配它们的背景。我想我可能会改变这两行的选择颜色以匹配它们的背景。我想我可能会改变这两行的选择颜色匹配他们的背景。谢谢,kleopatra。
listView.getProperties().put(“selectFirstRowByDefault”,false)代码>现在正在工作。谢谢,kleopatra。listView.getProperties().put(“selectFirstRowByDefault”,false)代码>现在正在工作。谢谢,kleopatra。listView.getProperties().put(“selectFirstRowByDefault”,false)代码>现在正在工作。谢谢,kleopatra。listView.getProperties().put(“selectFirstRowByDefault”,false)代码>目前正在工作。