JavaFX 2.2:如何强制重写/更新ListView [英] JavaFX 2.2: How to force a redraw/update of a ListView

查看:632
本文介绍了JavaFX 2.2:如何强制重写/更新ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaFX 2模式对话框窗口中有一个ListView控件。

I have a ListView control in a JavaFX 2 modal dialog window.

此ListView显示DXAlias实例,其列表单元由单元工厂制造。工厂对象所做的主要工作是检查ListView的UserData属性数据,并将其与ListCell对应的项进行比较。如果它们相同,则ListCell的内容将呈现为红色,否则为黑色。我这样做是为了指示ListView中的哪些项目当前被选为默认。这是我的ListCell工厂类,所以你可以看到我的意思:

This ListView displays DXAlias instances, the ListCells for which are manufactured by a cell factory. The main thing the factory objects do is examine the UserData property data of the ListView and compare it to the item corresponding to the ListCell. If they are the same, the contents of the ListCell are rendered in red, otherwise black. I do this to indicate which of the items in the ListView is currently selected as the "default". Here is my ListCell factory class so you can see what I mean:

private class AliasListCellFactory implements 
    Callback<ListView<DXSynonym>, ListCell<DXSynonym>> {

@Override
public ListCell<DXSynonym> call(ListView<DXSynonym> p) {
    return new ListCell<DXSynonym>() {

    @Override
    protected void updateItem(DXSynonym item, boolean empty) {
        super.updateItem(item, empty);

        if (item != null) {
            DXSynonym dx = (DXSynonym) lsvAlias.getUserData();

            if (dx != null && dx == item) {
                this.setStyle("-fx-text-fill: crimson;");                    
            } else { this.setStyle("-fx-text-fill: black;"); }

            this.setText(item.getDxName());

        } else { this.setText(Census.FORMAT_TEXT_NULL); }
    }};
}

我有一个名为handleAliasDefault()的按钮处理程序,它会生成所选项目在ListView中,通过获取所选的DXAlias实例并将其存储到ListView:lsvAlias.setUserData(选择的DXAlias)中的新默认值。这是处理程序代码:

I have a button handler called "handleAliasDefault()" which makes the selected item in the ListView the new default by taking the selected DXAlias instance and storing it into the ListView: lsvAlias.setUserData( selected DXAlias ). Here is the handler code:

// Handler for Button[fx:id="btnAliasDefault"] onAction
    @FXML
    void handleAliasDefault(ActionEvent event) {

        int sel = lsvAlias.getSelectionModel().getSelectedIndex();
        if (sel >= 0 && sel < lsvAlias.getItems().size()) {
            lsvAlias.setUserData(lsvAlias.getItems().get(sel));
        }
    }

因为点击时所做的更改Set Default按钮用于更改ListView的UserData()而不对ObservableList进行任何更改,列表未正确指示新的默认值。

Because the change that is made in response to clicking on the Set Default button is to change the ListView's UserData() without any change to the backing ObservableList, the list does not correctly indicate the new default.

有没有办法强制ListView重新呈现其ListCells? Android用户就此问题提出了数千万的问题,但JavaFX似乎并不幸福。我可能不得不对支持数组进行无意义的更改以强制重绘。

Is there a way to force a ListView to re-render its ListCells? There are a quadrillion questions from Android users on this subject, but there appears to be no happiness for JavaFX. I may have to make a "meaningless change" to the backing array to force a redraw.

我看到这被要求提供JavaFX 2.1: Javafx ListView刷新

I see that this was asked for JavaFX 2.1: Javafx ListView refreshing

推荐答案

现在,我可以在我的按钮处理程序中使用以下方法(称为forceListRefreshOn())重新绘制ListView并正确指示所选的默认值:

For now, I am able to get the ListView to redraw and correctly indicate the selected default by using the following method, called forceListRefreshOn(), in my button handler:

@FXML
void handleAliasDefault(ActionEvent event) {

    int sel = lsvAlias.getSelectionModel().getSelectedIndex();
    if (sel >= 0 && sel < lsvAlias.getItems().size()) {
        lsvAlias.setUserData(lsvAlias.getItems().get(sel));
        this.<DXSynonym>forceListRefreshOn(lsvAlias);
    }
}

帮助器方法只是从ListView中交换出ObservableList然后将其交换回来,可能强迫ListView更新其ListCells:

The helper method just swaps out the ObservableList from the ListView and then swaps it back in, presumably forcing the ListView to update its ListCells:

private <T> void forceListRefreshOn(ListView<T> lsv) {
    ObservableList<T> items = lsv.<T>getItems();
    lsv.<T>setItems(null);
    lsv.<T>setItems(items);
}

这篇关于JavaFX 2.2:如何强制重写/更新ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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