From 36b2c51787f485cce11d9abe6365aa35ea1341e5 Mon Sep 17 00:00:00 2001 From: Nathan McRae Date: Sun, 5 Jan 2025 13:25:51 -0800 Subject: [PATCH] Store StationSettings directly in ListView --- .../NumbersStationSelectionController.java | 34 +++++++++++++------ .../name/nathanmcrae/StationSettings.java | 5 +++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main/java/name/nathanmcrae/NumbersStationSelectionController.java b/src/main/java/name/nathanmcrae/NumbersStationSelectionController.java index 2a2644f..9ba779b 100644 --- a/src/main/java/name/nathanmcrae/NumbersStationSelectionController.java +++ b/src/main/java/name/nathanmcrae/NumbersStationSelectionController.java @@ -3,6 +3,7 @@ package name.nathanmcrae; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; +import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.control.TextInputDialog; import javafx.event.ActionEvent; @@ -15,9 +16,9 @@ import java.util.Optional; public class NumbersStationSelectionController { @FXML - private ListView stationListView; + private ListView stationListView; - private ObservableList stationList; + private ObservableList stationList; public NumbersStationSelectionController() { // Initialize the list with an empty list @@ -26,15 +27,24 @@ public class NumbersStationSelectionController { @FXML private void initialize() { + // Set custom cell factory to display station name + stationListView.setCellFactory(param -> new ListCell() { + @Override + protected void updateItem(StationSettings item, boolean empty) { + super.updateItem(item, empty); + if (empty || item == null || item.getName() == null) { + setText(null); + } else { + setText(item.getName()); + } + } + }); } public void setSettings(NumbersStationSettings settings) { // Populate the list with stations from settings - for (StationSettings station : settings.getStations()) { - System.out.println("Adding station: " + station.getName()); - stationList.add(station.getName()); - stationListView.setItems(stationList); - } + stationList.addAll(settings.getStations()); + stationListView.setItems(stationList); } @FXML @@ -49,7 +59,9 @@ public class NumbersStationSelectionController { Optional result = dialog.showAndWait(); result.ifPresent(stationName -> { // Add the new station to the list - stationList.add(stationName); + StationSettings newStation = new StationSettings(); + newStation.setName(stationName); + stationList.add(newStation); }); } @@ -66,11 +78,11 @@ public class NumbersStationSelectionController { private void handleSelectButtonPress(Event e) { Node node = (Node) e.getSource(); Stage stage = (Stage) node.getScene().getWindow(); - String selectedStation = stationListView.getSelectionModel().getSelectedItem(); + StationSettings selectedStation = stationListView.getSelectionModel().getSelectedItem(); if (selectedStation != null) { - stage.setUserData(selectedStation); - System.out.println("Selected Station: " + selectedStation); + stage.setUserData(selectedStation.getName()); + System.out.println("Selected Station: " + selectedStation.getName()); } stage.close(); } diff --git a/src/main/java/name/nathanmcrae/StationSettings.java b/src/main/java/name/nathanmcrae/StationSettings.java index 6d14494..51fe0b1 100644 --- a/src/main/java/name/nathanmcrae/StationSettings.java +++ b/src/main/java/name/nathanmcrae/StationSettings.java @@ -28,4 +28,9 @@ public class StationSettings { public void setMessageLength(int newMessageLength) { messageLength = newMessageLength; } + + @Override + public String toString() { + return name; + } }