ListView无法使用JavaFX FXML正确更新

时间:2017-10-02 03:31:09

标签: java listview javafx

在JavaFX中,我使用ListView显示在Set中添加或删除的项目。我已经使用了一个observableSet来与ListView一起使用来显示更新,但是当Set更改时ListView没有正确更新。这是我的代码与解决方案。但为什么它没有按预期工作?

//MARK:- WKUIDelegate
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    NSLog(#function)

    if navigationAction.targetFrame == nil {
        NSLog("=> Create a new webView")

        let webView = WKWebView(frame: self.view.bounds, configuration: configuration)
        webView.uiDelegate = self
        webView.navigationDelegate = self

        self.webView = webView

        return webView
    }
    return nil
}

问题: 每次更新observableSet时,我都要按照上面和下面的说明继续设置ListView。否则,更改不会在ListView中显示。

public class FXMLDocumentController implements Initializable {
    @FXML
    private ListView listView;
    ObservableSet<String> observableSet; //ObservableSet to prevent dublicates
    Integer i = 3;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        observableSet = FXCollections.observableSet();
        observableSet.addAll(Arrays.asList("Item1", "Item2", "Item3")); 
        //Setting up ListView to the ObservableSet
        listView.setItems(FXCollections.observableArrayList(observableSet));

    }
    @FXML
    private void handleButtonAction(ActionEvent event) {
        observableSet.add("Item" + i++);
       //Setting up ListView to the ObservableSet otherwise it won't update
       //My understanding that I don't have to do this with an observableSet
        listView.setItems(FXCollections.observableArrayList(observableSet));
    }
    @FXML
    private void handleRemoveAction(ActionEvent event) {
       observableSet.removeAll(listView.getSelectionModel().getSelectedItems());
       //Setting up ListView to the ObservableSet otherwise it won't update
       listView.setItems(FXCollections.observableArrayList(observableSet));
    }
}

2 个答案:

答案 0 :(得分:1)

这是预期的行为。您要调用create the observable list的方法“创建一个新的可观察列表并将[该集合]的内容添加到其中”。因此对集合的后续更改不会更改列表。

另一种选择就是用可观察集注册一个监听器,并通过适当地添加或删除一个元素来更新列表视图的项目:

public class FXMLDocumentController implements Initializable {
    @FXML
    private ListView listView;
    private ObservableSet<String> observableSet; //ObservableSet to prevent dublicates
    private int i = 3;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        observableSet = FXCollections.observableSet();

        observableSet.addListener((SetChangeListener.Change<? extends String> c) -> {
            if (c.wasAdded()) {
                listView.getItems().add(c.getElementAdded());
            }
            if (c.wasRemoved()) {
                listView.getItems().remove(c.getElementRemoved());
            }
        });

        observableSet.addAll(Arrays.asList("Item1", "Item2", "Item3")); 

    }
    @FXML
    private void handleButtonAction(ActionEvent event) {
        observableSet.add("Item" + i++);
    }
    @FXML
    private void handleRemoveAction(ActionEvent event) {
       observableSet.removeAll(listView.getSelectionModel().getSelectedItems());
    }
}

答案 1 :(得分:0)

尝试拨打

listview.refresh();
在列表中添加和删除项目后

或者您可以在列表中添加更改侦听器并从中调用refresh方法。 但我更喜欢第一种方法,因为添加监听器有时不会更新列表视图。