JAVAFX 列表视图刷新选项卡

JAVAFX LISTVIEW REFRESH TAB

我被困在学校项目中。

我有 javafx 项目,有控制器 A 和控制器 B。 A 控制器与选项卡 A 关联,B 控制器与选项卡 B 关联。

在两个控制器中,我都有一个列出相同对象的列表视图(例如@FXML Listview moviesList)。

在选项卡 A 中,我可以从列表视图中删除一部电影。没问题,对象已从列表视图中删除。

但问题是,当我打开选项卡 B 时,对象仍在列表视图中。

而且我不知道删除对象后如何刷新这个列表视图。

Main.java

public class Main extends Application {

private Stage primaryStage;
private TabPane rootLayout;
private Tab tabMovie = new Tab("Movies");
private Tab tabRoom = new Tab("Rooms");


@Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Javaravi Admin");
        primaryStage.setResizable(false);

        loadScreen();

        showMovies();

        showRoomsAndShows();


    }


public void loadScreen() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("menu.fxml"));
            rootLayout = fxmlLoader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            scene.getStylesheets().add(Main.class.getResource("style.css").toExternalForm());
            rootLayout.getTabs().addAll(tabMovie, tabRoom, tabParam);
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }


public void showMovies() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("movie.fxml"));
            AnchorPane filmView = fxmlLoader.load();

            // Set person overview into the center of root layout.

            tabMovie.setContent(filmView);

            // Give the controller access to the main app.
            MovieController controller = fxmlLoader.getController();


        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }



public void showRoomsAndShows() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("system.fxml"));
            AnchorPane room = fxmlLoader.load();

            // Set person overview into the c enter of root layout.
            tabRoom.setContent(room);

            // Give the controller access to the main app.
            SystemController systemController = fxmlLoader.getController();


        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

MovieController.java

public class MovieController {

    @FXML
    private ListView<Film> moviesListView;

    private Database db = Database.getInstance();
    private ModelFactory mf = ModelFactory.getInstance();

    public MovieController() {
    }

    private void showAllMovies() {

        List<Film> moviesList = new ArrayList<>();
        List<Model> modelsList = mf.getAllModels("Film");
        for (int i=0; i<modelsList.size(); i++) {
            moviesList.add((Film) modelsList.get(i));
        }
        ObservableList<Film> myObservableList = FXCollections.observableList(moviesList);
        setMoviesItems(myObservableList);
    }

    private void setMoviesItems(ObservableList<Film> item) {

        moviesListView.setItems(item);
    }

@FXML
    private void initialize() {

        showAllMovies();
    }

    @FXML
    private void deleteShow() {

        Film selectedFilm = moviesListView.getSelectionModel().getSelectedItem();

            System.out.println("Validation suppression");
            selectedFilm.removeFromDb();
            showAllMovies();
            moviesListView.requestFocus();
            moviesListView.getSelectionModel().select(0);
            getSelectedMovie();      

    }

Show.java

public class 系统控制器 {

@FXML
private ListView<Film> selectedMovieResa;

@FXML
    private void initialize() {

        // initialize lists
        showAllLanguages();
        showAllShows();
        showAllMovies();
        showAllRooms();
    }

// set movies list
    private void showAllMovies() {

        // same process as the first one (line 158, set room list)
        List<Film> moviesList = new ArrayList<>();
        List<Model> modelsList = mf.getAllModels("Film");
        for (int i=0; i<modelsList.size(); i++) {
            moviesList.add((Film) modelsList.get(i));
        }
        ObservableList<Film> myObservableList = FXCollections.observableList(moviesList);
        setMoviesItems(myObservableList);
    }

    public void setMoviesItems(ObservableList<Film> item) {

        selectedMovieResa.setItems(item);
        movieFromShow.setItems(item);
        filterMovie.setItems(item);
    }

谢谢!

问题是您的每个 ListView 都包含不同的 ObservableList 数据。因此,从一个列表中删除不会从另一个列表中删除。最好的解决方案是让两个列表视图共享一个数据列表。

让您的 ModelFactory class 通过 getMovies() 方法创建(单个)ObservableList<Model> 和 return。像

public class ModelFactory {

    private ObservableList<Film> filmList = null ;

    // existing code...

    public ObservableList<Film> getMovies() {
        if (filmList == null) {
            List<Film> films = new ArrayList<>();
            List<Model> models = getAllModels("Film");
            for (Model m : models) {
                films.add((Film)m);
            }
            filmList = FXCollections.observableList(films);
        }
        return filmList ;
    }

    // ...
}

那么您就只有一个列表,可以由各种视图共享:

public class SystemController {

    // ...

    public void showAllMovies() {
        setMoviesItems(mf.getMovies());
    }

    // ...
}

public class MoviesController {

    // ...

    public void showAllMovies() {
        setMoviesItems(mf.getMovies());
    }

    // ...

    @FXML
    private void deleteShow() {
        Film selectedFilm = moviesListView.getSelectionModel().getSelectedItem();
        selectedFilm.removeFromDb();
        // since list is shared, this will be observed in all views...
        moviesListView.getItems().remove(selectedFilm); 
    }

    // ...
}

https://github.com/james-d/SharedListView

中有一个类似的、更简单(但完整)的示例