How to call another screen through the Menuitem

Asked

Viewed 498 times

1

I’m trying to call another screen (include) through a Menuitem, but it’s not working.

At first, I can call my Login screen from Main.java, then tbm can log in and call the Menu screen via a button.

I believe Menuitem’s method is different from Button’s, but I can’t do it.

 package application;

 import java.awt.event.ActionListener; import java.io.IOException;

 import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import
 javafx.scene.Scene; import javafx.scene.control.MenuItem; import
 javafx.stage.Stage;

 public class MenuController {

 @FXML private MenuItem menuItemFechar;

 @FXML private MenuItem menuItemIncluir;

 @FXML private MenuItem menuItemConsultar;

 @FXML private MenuItem menuItemAlterar;

 @FXML private MenuItem menuItemDeletar;

 @FXML public void logar(ActionListener action) {

   try {

    // alternar tela (fecha tela menu e abre tela incluir)  
    menuItemIncluir.getScene().getWindow().hide();

    FXMLLoader fxmlLoader = new FXMLLoader();  
    fxmlLoader.setLocation(getClass().getResource("Incluir.fxml"));  
    Scene scene = new Scene(fxmlLoader.load(), 600, 400);       
    Stage stage = new Stage();      
    stage.setTitle("Incluir");      
    stage.setScene(scene);      
    stage.show();   

   }  catch (IOException e) {       
     e.printStackTrace();   
   }

 } 

 }
  • Menuitem uses the same mechanism to perform an action (setOnAction). The problem is the screen exchange methodology you are using.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.