如何将ObservableList与ListView一起使用?

时间:2017-10-29 18:19:40

标签: java javafx

即使在阅读了Oracle上的所有ListView文档后,我也很难实现最简单的ListView。 https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ListView.html

如何让ListView processingListView自动反映ObservableList processingList的内容?似乎ListView完全忽略了我提供的ObservableList,而是使用了一些只能通过getItems方法访问的空列表。请在下面的代码中查看评论以获取更多详细信息。

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.util.List;

public class MainController {
    @FXML
    private Button addItemsToProcListButton;

    private ObservableList<File> processingList = FXCollections.observableArrayList(
            new File("/Users/matt/Dropbox/Matthew/chart.png") // Example file doesn't show up when loaded
    );

    @FXML
    private ListView<File> processingListView = new ListView<>(processingList);

    @FXML
    private void addItemsToProcessingList() {
        final Stage stage = (Stage) addItemsToProcListButton.getScene().getWindow();
        final FileChooser fileChooser = new FileChooser();

        fileChooser.setTitle("Add Images to Processing List");
        fileChooser.getExtensionFilters().addAll(
            new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif")
        );

        App.logger.info("processingList size = " + processingList.size()); // Shows expected size
        App.logger.info("listViewList size = " + processingListView.getItems().size()); // Always 0

        List<File> files = fileChooser.showOpenMultipleDialog(stage);
        processingList.addAll(files);

        App.logger.info("processingList size = " + processingList.size()); // Increases as expected
        App.logger.info("listViewList size = " + processingListView.getItems().size()); // Always 0

        // I can see items if I manually add them but this doesn't follow the suggested pattern
        // processingListView.setItems(processingList);

        // Updates but is not recommended https://docs.oracle.com/javafx/2/api/javafx/scene/control/ListView.html
        // processingListView.getItems().addAll(files);
    }
}

更新的解决方案

当我在FXML文件中使用了相应的标记时,我错误地在我的控制器中为我的ObservableList和ListView创建了新对象。

我将课程改为以下

import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.util.List;

public class MainController {
    @FXML
    private Button addItemsToProcListButton;

    @FXML
    private ObservableList<File> processingList;

    @FXML
    private ListView<File> processingListView;

    @FXML
    private void addItemsToProcessingList() {
        final Stage stage = (Stage) addItemsToProcListButton.getScene().getWindow();
        final FileChooser fileChooser = new FileChooser();

        fileChooser.setTitle("Add Images to Processing List");
        fileChooser.getExtensionFilters().addAll(
            new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif")
        );

        final List<File> files = fileChooser.showOpenMultipleDialog(stage);
        processingList.addAll(files);
    }
}

我更改了以前的FXML文件

<ListView prefWidth="300.0" fx:id="processingListView" />

<ListView prefWidth="300.0" fx:id="processingListView">
    <items>
        <FXCollections fx:factory="observableArrayList" fx:id="processingList"></FXCollections>
    </items>
</ListView>

0 个答案:

没有答案