JAVAFX LISTVIEW REFRESH TAB

时间:2015-05-28 20:02:26

标签: listview javafx controller reload

我被困在学校项目中。

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

在两个控制器中,我都有一个listview列出了同一个对象(例如@FXML Listview moviesList)。

在标签A中,我可以从列表视图中删除电影。没问题,该对象将从列表视图中删除。

但问题是,当我打开Tab B时,对象仍然在Listview中。

在删除对象后,我不知道如何刷新此列表视图。

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 SystemController {

@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);
    }

Tks!

1 个答案:

答案 0 :(得分:1)

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

让您的ModelFactory班级创建(单个)ObservableList<Model>并通过getMovies()方法返回。像

这样的东西
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