Main class:

@Override public void start(Stage stage) throws Exception { controllerCreateNewBuildMods = new ControllerCreateNewBuildMods(); System.out.println(" Program start!"); System.out.println(""); storageVariables = new StorageVariables(); language=storageVariables.getLanguage(); while (aBoolean == true){ try{ icons.add(new Image(getClass().getResource("res//icons//" + i + ".png").toString())); i++; }catch(Exception e){ i--; System.err.println(" *НайдСно " + i + " ΠΈΠΊΠΎΠ½ΠΊΠΈ"); amount = i; break; } } System.out.println(amount); controllerCreateNewBuildMods.setAmount(amount);//Π—Π΄Π΅ΡΡŒ Π²Π°ΠΆΠ½ΠΎ fXMLLoader_MainMenu = new FXMLLoader(); fXMLLoader_MainMenu.setLocation(getClass().getResource("MainChoiceTheBuildMenu.fxml")); fXMLLoader_MainMenu.setResources(ResourceBundle.getBundle("sample.Properties.MainMenu", new Locale(language))); Parent panel = FXMLLoader.load(getClass().getResource("MainChoiceTheBuildMenu.fxml")); Scene scene=new Scene(panel,700,500); stageMainMenu=new Stage(); stageMainMenu.setScene(scene); stageMainMenu.setTitle(fXMLLoader_MainMenu.getResources().getString("key_MainMenu_Title")); //titleMainMenu stageMainMenu.show(); } 

Class ControllerCreateNewBuildMods:

 @FXML ImageView imgCreateNewBuildModsIconBuildMods; private int amount; private int position = 1; private String stringLanguage; private Main main = new Main(); private StorageVariables storageVariables=new StorageVariables(); private Image img; public void setAmount(int amount){ this.amount = amount; System.out.println(amount);//Π—Π΄Π΅ΡΡŒ Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ 135(Π’Π°ΠΊ ΠΈ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π±Ρ‹Ρ‚ΡŒ) System.out.println(this.amount);//Π—Π΄Π΅ΡΡŒ Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ 135(Π’Π°ΠΊ ΠΈ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π±Ρ‹Ρ‚ΡŒ) } // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ GUI ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚ΠΎΠ² класса 'CreateNewBuildMods' ΠΏΠΎΠ΄ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ язык @Override public void initialize(URL url, ResourceBundle resourceBundle) { amount = main.getAmount(); img = new Image(getClass().getResource("res//icons//"+position+".png").toString()); stringLanguage=storageVariables.getLanguage(); ResourceBundle lngBndl = ResourceBundle .getBundle("sample.Properties.MainMenu", new Locale(stringLanguage)); imgCreateNewBuildModsIconBuildMods.setImage(img); } /* ΠœΠ΅Ρ‚ΠΎΠ΄Ρ‹ - ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π»Π΅Ρ€Ρ‹ для ΠΊΠ½ΠΎΠΏΠΊΠΈ 'btnBackImage' */ // Π­Ρ‚ΠΎΡ‚ ΠΌΠ΅Ρ‚ΠΎΠ΄ - ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π»Π΅Ρ€ ΠΊΠ½ΠΎΠΏΠΊΠΈ 'btnBackImage' ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅Ρ‚ Π½Π°ΠΆΠ°Ρ‚ΠΈΠ΅ ΠΊΠ½ΠΎΠΏΠΊΠΈ public void inquiryQuestionBtnBackImage() { if(position == 1){ } position = amount; System.out.println(position); //Π—Π΄Π΅ΡΡŒ Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ 0, хотя Π΄ΠΎΠ»ΠΆΠ΅Π½ 135 img = new Image(getClass().getResource("res//icons//"+position+".png").toString());//Π—Π΄Π΅ΡΡŒ Π²Ρ‹Π΄Π°Π΅Ρ‚ ΠΎΡˆΠΈΠ±ΠΊΡƒ, Ρ‚Π°ΠΊ - ΠΊΠ°ΠΊ Π½Π΅Ρ‚ Ρ„Π°ΠΉΠ»Π° 0 imgCreateNewBuildModsIconBuildMods.setImage(img); } 

I do not understand why such garbage. Tell me please. I'm a newbie

  • And where is the same button code? Can you take a look at this action? - Alexey Shimansky
  • Description of the button in the fxml file. The point, I think not in this. I really do not understand why the variable amount first stored 135, and then began to store 0 - dhred
  • The method inquiryQuestionBtnBackImage handles clicking on this button - dhred
  • you are not doing something there. At a minimum, the controller can call the public void initialize() method in which to describe initialization, including setting the amount , i.e. in the controller, write public void initialize() { amount = 135; } public void initialize() { amount = 135; } - Alexey Shimansky
  • This number is the result of this code: while (aBoolean == true) {try {icons.add (new Image (getClass (). GetResource ("res // icons //" + i + ".png"). ToString ())); i ++; } catch (Exception e) {i--; System.err.println ("* Found" + i + "icons"); amount = i; break; }} And it can change, because of what I can't do that - dhred

1 answer 1

When you do new ControllerCreateNewBuildMods(); - you create a completely different controller instance. Not the one to which the button β€œaccesses” when pressed.

You can try to contact the controller from the FXMLLoader and do the necessary.

 FXMLLoader fxmlLoader = new FXMLLoader(); Pane p = fxmlLoader.load(getClass().getResource("foo.fxml").openStream()); FooController fooController = (FooController) fxmlLoader.getController(); 

Specifically, in your code in Main will look something like this:

 @Override public void start(Stage stage) throws Exception { // Π—Π΄Π΅ΡΡŒ ΠΊΠ°ΠΊΠΈΠ΅-Ρ‚ΠΎ дСйствия, Π²Π°ΠΌ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΡ‹Π΅ // ... fXMLLoader_MainMenu = new FXMLLoader(); fXMLLoader_MainMenu.setLocation(getClass().getResource("MainChoiceTheBuildMenu.fxml")); fXMLLoader_MainMenu.setResources(ResourceBundle.getBundle("sample.Properties.MainMenu", new Locale(language))); //Π­Ρ‚ΠΎ Π½Π΅ Π½ΡƒΠΆΠ½ΠΎ! ->>>> Parent panel = FXMLLoader.load(getClass().getResource("MainChoiceTheBuildMenu.fxml")); Parent panel = (Parent) fXMLLoader_MainMenu.load(); // А здСсь обращаСмся ΠΊ ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π»Π΅Ρ€Ρƒ ΠΈ Π΄Π΅Π»Π°Π΅ΠΌ наши Π΄Π΅Π»Π° controllerCreateNewBuildMods = fXMLLoader_MainMenu.getController(); controllerCreateNewBuildMods.setAmount(666); // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌ "Π‘Ρ†Π΅Π½Ρƒ" Scene scene = new Scene(panel,700,500); stageMainMenu = new Stage(); stageMainMenu.setScene(scene); stageMainMenu.setTitle(fXMLLoader_MainMenu.getResources().getString("key_MainMenu_Title")); //titleMainMenu stageMainMenu.show(); } 

But in general, initialization is better to do where necessary. For example, directly in the desired controller, or make the main MainController and do all the necessary initialization there. How it's done?

There is a method like initialize . It starts automatically in the controller (if it is present there) during initialization, respectively. You can configure everything there.

For example, we declare a method in the controllerCreateNewBuildMods :

 public void initialize() { // do smth }