The application is something like a dictionary. There is a menu of 4 buttons: add, load, delete and exit. After clicking on the first three buttons, a dialog box opens and then loads ... a dictionary. For now, I just bring to the console which button was pressed and here is the problem: when I clicked on add, it shows that all buttons were pressed, when I clicked on download - two, and only when I clicked on delete, it shows that only the delete button was pressed. Add top button, remove bottom button. I can not understand what's wrong. Here is my event handler.

package controllers; import javafx.event.ActionEvent; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.stage.Stage; import java.io.IOException; public class menuStageController { enum MenuItem { add, download, delete } private static MenuItem menuItem; public void bAddDictionaryPressed(ActionEvent actionEvent) { menuItem = MenuItem.add; this.dialogStageShow(); } public void bDownloadDictionaryPressed(ActionEvent actionEvent) { menuItem = MenuItem.download; this.dialogStageShow(); } public void bDeleteDictionaryPressed(ActionEvent actionEvent) { menuItem = MenuItem.delete; this.dialogStageShow(); } public void exitAction(ActionEvent actionEvent) { System.exit(0); } public void dialogStageShow() { Stage dialogStage = new Stage(); Parent loadScene = null; try { loadScene = FXMLLoader.load(getClass().getResource("../fxml/dialogStage.fxml")); } catch (IOException e) { e.printStackTrace(); } dialogStage.setScene(new Scene(loadScene)); dialogStage.setResizable(false); dialogStage.show(); } public void dialogStageAction(ActionEvent actionEvent) { switch(menuItem) { case add: System.out.println("Add pressed"); case download: System.out.println("Download pressed"); case delete: System.out.println("Delete pressed"); } } } 

    1 answer 1

    In your switch-case design:

     switch(menuItem) { case add: System.out.println("Add pressed"); case download: System.out.println("Download pressed"); case delete: System.out.println("Delete pressed"); } 

    not enough operator break . Because of this, all case blocks are executed after a suitable condition .

    The correct construction will look like this:

     switch(menuItem) { case add: System.out.println("Add pressed"); break; case download: System.out.println("Download pressed"); break; case delete: System.out.println("Delete pressed"); break; } 

    Here, after the block that fits the condition, the break statement will be executed, as a result of which the execution of the program will continue immediately after the switch-case construction, bypassing all subsequent case this construction.

    • You can ask another question. Why, if menuItem is declared non-static, gives a NullPointerException error? How is it that menuItem is not initialized? In any case, I first click on one of the buttons, and then open the dialogStage. There is an assumption that the matter is in flows in which I am not yet strong. - not a Programmer
    • @DenisSavenko, It’s hard to say without seeing the whole code. It is better to create a new question, where to put all the available information. - post_zeew