Answer the question
In order to leave comments, you need to log in
Why doesn't the window controller see the list from the main class?
Generally. Such situation.
I pass from the main class to the class-controller of the settings window a list with the values set earlier in the main class through the constructor. Here is the main class code:
public class MainClass extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private Settings settings;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Антикафе");
initRootLayout();
showPersonOverview();
}
private ObservableList<Settings> Data = FXCollections.observableArrayList();
public MainClass() {
Data.add(new Settings(false,false,false,false));
}
public ObservableList<Settings> getData() {
return Data;
}
public boolean showSettingsDialog() {
try {
// Загружаем fxml-файл и создаём новую сцену
// для всплывающего диалогового окна.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainClass.class.getResource("view/settings.fxml"));
AnchorPane page = (AnchorPane) loader.load();
// Создаём диалоговое окно Stage.
Stage dialogStage = new Stage();
dialogStage.setTitle("Настройки");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Передаём список настроек в контроллер.
ButtonSettingsControll controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setData(Data);
controller = new ButtonSettingsControll(Data);
// Отображаем диалоговое окно и ждём, пока пользователь его не закроет
dialogStage.showAndWait();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
public class ButtonSettingsControll {
private Stage dialogStage;
@FXML
public CheckBox checkKal;
@FXML
public CheckBox checkStopCheck;
@FXML
public CheckBox checkTea;
@FXML
public CheckBox checkVIP;
@FXML
public TextField kalHard;
@FXML
public TextField kalNorm;
@FXML
public TextField kalLight;
@FXML
public TextField priceTea;
@FXML
public TextField priceUnd;
@FXML
public TextField peoplsUndo;
@FXML
public TextField priceTo;
@FXML
public TextField peoplsTo;
@FXML
public TextField priceStopCheck;
private MainClass mainClass;
private ObservableList<Settings> Data = FXCollections.observableArrayList();
public void setMain(MainClass mainClass) {
this.mainClass = mainClass;
}
public void setData(ObservableList<Settings> data) {
Data = data;
}
//Пока не разберусь со списком для наглядности буду использовать это
Boolean CK = false;
Boolean CT = false;
Boolean CV = false;
Boolean CC = false;
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
public ButtonSettingsControll() {
}
public ButtonSettingsControll(ObservableList<Settings> data) {
Data = data;
System.out.println(Data.get(0).getCheckK());
checkKal.setSelected(data.get(0).getCheckK());
}
@FXML
private void initialize() {
if(CK)
checkKal.setSelected(true);
else {
checkKal.setSelected(false);
kalHard.setDisable(true);
kalNorm.setDisable(true);
kalLight.setDisable(true);
}
if(CT)
checkTea.setSelected(true);
else {
checkTea.setSelected(false);
priceTea.setDisable(true);
}
if(CV)
checkVIP.setSelected(true);
else {
checkVIP.setSelected(false);
priceUnd.setDisable(true);
priceTo.setDisable(true);
peoplsTo.setDisable(true);
peoplsUndo.setDisable(true);
}
if(CC)
checkStopCheck.setSelected(true);
else {
checkStopCheck.setSelected(false);
priceStopCheck.setDisable(true);
}
}
@FXML
private void settings() {
Data.setAll(new Settings(checkKal.isSelected(),checkTea.isSelected(),checkVIP.isSelected(),checkStopCheck.isSelected()));
System.out.println(Data.get(0).getCheckK());
dialogStage.close();
}
}
public ButtonSettingsControll(ObservableList<Settings> data) {
Data = data;
System.out.println(Data.get(0).getCheckK());
checkKal.setSelected(Data.get(0).getCheckK());
}
checkKal.setSelected(data.get(0).getCheckK());.
Answer the question
In order to leave comments, you need to log in
here is an error.
// Передаём список настроек в контроллер.
ButtonSettingsControll controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setData(Data);
controller = new ButtonSettingsControll(Data);
ButtonSettingsControll controller = loader.getController();
received a link to the controller from the loader. controller = new ButtonSettingsControll(Data);
created a new object not related to the loaded FXML Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question