How do I pass the selected ListView value from one controller to another? This is the first
Question:
How do I pass the selected ListView value from one controller to another? This is the first controller, which I want to be able to pass the selected item to the details view when detailsButtonClicked action event occurs.
{ @FXML private BorderPane borderPane; @FXML private ImageView imageView; @FXML private ListView resultsListView; @FXML private TextField searchTextField; @FXML private Button detailsButton; @FXML void detailsButtonClicked(ActionEvent event) throws IOException { var posterNotFoundImage = new Image(getClass().getResourceAsStream("poster-not-found.png")); resultsListView.getSelectionModel().selectedItemProperty().addListener((obs, oldMovieSelected, newMovieSelected) ->{ System.out.println(newMovieSelected); try { SceneManager.Instance().changeSceneWithMovie(event, "details-view.fxml", newMovieSelected.getIMDB_ID()); imageView.setImage(new Image(newMovieSelected.getPoster())); }catch(Exception e) { imageView.setImage(posterNotFoundImage); } }); } @FXML void searchButtonClicked(ActionEvent event) { var searchResults = APIManager.Instance().getMovieFromOMDBBySearchTerm(searchTextField.getText()); resultsListView.getItems().clear(); if(searchResults.getMovies() != null) { resultsListView.getItems().addAll(searchResults.getMovies()); resultsListView.getSelectionModel().select(0); } else { } } @FXML void searchTextFieldSubmitted(ActionEvent event) { searchButtonClicked(event); resultsListView.requestFocus(); } @Override public void initialize(URL url, ResourceBundle resourceBundle) { var posterNotFoundImage = new Image(getClass().getResourceAsStream("poster-not-found.png")); resultsListView.getSelectionModel().selectedItemProperty().addListener((obs, oldMovieSelected, newMovieSelected) ->{ System.out.println(newMovieSelected); try { imageView.setImage(new Image(newMovieSelected.getPoster())); }catch(Exception e) { imageView.setImage(posterNotFoundImage); } }); } }
This is the details view controller I want the value passed to
public class DetailsViewController { @FXML private ImageView imageView; @FXML public static TextArea resultView; @FXML private Button searchButton; @FXML void searchButtonClicked(ActionEvent event) throws IOException { SceneManager.Instance().changeScene(event, "search-view.fxml"); } public static String displayMovie(String ImdbId){ String s = "Hi"; var searchResults = APIManager.Instance().getMovieFromOMDBByID(ImdbId); resultView.clear(); resultView.setText(searchResults.toString()); return s; } }
My SceneManager works and I can switch between scenes using it I am just having trouble passing the value over to display movie details in the second view. Below is my APIManager that handles the API
public class APIManager { /********************* SINGLETON SECTION *****************************/ // Step 1. private static instance member variable private static APIManager m_instance = null; // Step 2. make the default constructor private private APIManager() {} // Step 3. create a public static entry point / instance method public static APIManager Instance() { // Step 4. Check if the private instance member variable is null if(m_instance == null) { // Step 5. Instantiate a new APIManager instance m_instance = new APIManager(); } return m_instance; } /*********************************************************************/ public Movie getMovieFromOMDBByTitleAndYear(String title, String year) { // Step 1. Construct your URL String APIKey = "f8758a18"; title = title.replace(" ", "%20"); String URL = "https://www.omdbapi.com/?apikey=" + APIKey + "&t=" + title + "&y=" + year; // Step 2. Create a Client Object HttpClient client = HttpClient.newHttpClient(); // Step 3. Create a Request Object HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create(URL)).build(); try { // Step 4. Create a Response object HttpResponse response = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); // Step 5. Create a Gson object Gson gson = new Gson(); // Step 6. Deserialize the data using the fromJSON method return gson.fromJson(response.body(), Movie.class); } catch(Exception exception) { exception.printStackTrace(); } return null; } public MovieSearchResults getMovieFromOMDBBySearchTerm(String searchTerm) { // Step 1. Construct your URL String APIKey = "769bf72f"; searchTerm = searchTerm.replace(" ", "%20"); String URL = "https://www.omdbapi.com/?apikey=" + APIKey + "&s=" + searchTerm; // Step 2. Create a Client Object HttpClient client = HttpClient.newHttpClient(); // Step 3. Create a Request Object HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create(URL)).build(); try { // Step 4. Create a Response object HttpResponse response = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); // Step 5. Create a Gson object Gson gson = new Gson(); // Step 6. Deserialize the data using the fromJSON method return gson.fromJson(response.body(), MovieSearchResults.class); } catch(Exception exception) { exception.printStackTrace(); } return null; } public Movie getMovieFromOMDBByID(String imdbID) { // Step 1. Construct your URL String APIKey = "769bf72f"; String URL = "https://www.omdbapi.com/?apikey=" + APIKey + "&i=" + imdbID; // Step 2. Create a Client Object HttpClient client = HttpClient.newHttpClient(); // Step 3. Create a Request Object HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create(URL)).build(); try { // Step 4. Create a Response object HttpResponse response = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); // Step 5. Create a Gson object Gson gson = new Gson(); // Step 6. Deserialize the data using the fromJSON method return gson.fromJson(response.body(), Movie.class); } catch(Exception exception) { exception.printStackTrace(); } return null; } }
The detailsButtonClicked method I wrote in the search view controller does not work and that is what I am looking for help with, to pass the selected item to the details view controller when that button is clicked
Fundamentals of Cost Accounting
ISBN: 978-0077398194
3rd Edition
Authors: William Lanen, Shannon Anderson, Michael Maher