I have a problem with filling a ListView
in my main window with objects per button event in another open window.
I tried several things, but it never seems to work unless I do it per button event in the actual window where the ListView
is located. I wondered if it had anything to do with using an instance of the controller for the main window, but I'm new to JavaFX and I don't really know what I'm doing, tbh.
Some additional information:
Infos is a class where Infos are stored that I need to access in more than one controller class;
gastConrim
is the method called in another open window (AddEditGastController
);
the last part is the part that works (updating it per button event in the main window where the ListView
is located) which would be similar to a "update ListView"-Button, but it's kinda inconvinient if you have to update the ListView
manually.
public abstract class Infos {
public static String ID;
static Hotelsystem ht = new Hotelsystem();
public static Hotelsystem getHt(){
return ht;
}
static void alert(String titel, String content) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(titel);
alert.setHeaderText(null);
alert.setContentText(content);
alert.show();
}
static boolean idAbfragen(String titel, String content, String alertContent) {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle(titel);
dialog.setHeaderText(null);
dialog.setContentText(content);
Optional<String> input = dialog.showAndWait();
if (!input.isPresent() || input.get().trim().isEmpty()) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(titel);
alert.setHeaderText(null);
alert.setContentText(alertContent);
alert.showAndWait();
return false;
}
else {
ID = input.get();
return true;
}
}
static ObservableList<Gast> gastData = FXCollections.observableArrayList();
public static void updateGast() {
gastData.setAll(ht.getGastList());
}
}
@FXML
void gastConfirm(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader= new FXMLLoader(getClass().getResource("../gui/mainWindowGast.fxml"));
Parent root = (Parent) fxmlLoader.load();
MWControllerGast controller = (MWControllerGast) fxmlLoader.getController();
double rabatt = 0.0;
if (gastNameInput.getText().trim().isEmpty() || anredeToggle.getSelectedToggle() == null || gastGB.getValue() == null || gastTfInput.getText().trim().isEmpty() ||
gastStrasseInput.getText().trim().isEmpty() || gastStadtInput.getText().trim().isEmpty() || gastLandInput.getText().trim().isEmpty()) {
Infos.alert("Fehler", "Es fehlen Informationen.");
}
else {
if (gastStatus.isSelected()) {
if (!gastRabattInput.getText().trim().isEmpty()) {
String rabattInput = gastRabattInput.getText();
if (rabattInput.contains(","))
rabattInput.replace(",", ".");
rabatt = Double.parseDouble(rabattInput);
Infos.getHt().getHinzufuegen().vipHinzufuegen(gastNameInput.getText(), gastAnredeRadio1.getText(), gastGB.getValue(), gastTfInput.getText(), gastStrasseInput.getText(), gastStadtInput.getText(), gastLandInput.getText(), rabatt);
}
Infos.ht.getHinzufuegen().vipHinzufuegen(gastNameInput.getText(), gastAnredeRadio1.getText(), gastGB.getValue(), gastTfInput.getText(), gastStrasseInput.getText(), gastStadtInput.getText(), gastLandInput.getText(), rabatt);
} else
Infos.ht.getHinzufuegen().gastHinzufügen(gastNameInput.getText(), gastAnredeRadio1.getText(), gastGB.getValue(), gastTfInput.getText(), gastStrasseInput.getText(), gastStadtInput.getText(), gastLandInput.getText());
System.out.println(rabatt);
gastNameInput.clear();
gastAnredeRadio1.setSelected(false);
gastAnredeRadio2.setSelected(false);
gastGB.setValue(null);
gastTfInput.clear();
gastStrasseInput.clear();
gastStadtInput.clear();
gastLandInput.clear();
gastStatus.setSelected(false);
gastRabattInput.clear();
Infos.updateGast();
controller.gastTable.getItems().setAll(Infos.gastData);
}
@FXML
ListView<Gast> gastTable;
public ListView<Gast> getGastTable() {
return gastTable;
}
@FXML
void sortieren(ActionEvent event) {
Infos.updateGast();
gastTable.getItems().setAll(Infos.gastData);
}
The expected result: Update the ListView
in gastConfirm
whenever I add a new Object.
The actual result: Nothing happens. At least nothing I can see in the console or user interface. It just doesn't add the object to the ListView
.