Results 1 to 6 of 6
  1. #1
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    32
    Rep Power
    0

    Default JavaFx Controller set Object Another Controller

    I'am gettin Admin object in LoginController. Its not null there is no problem. When i setting this object AdminController its not working. In my initialize syso result showing "null". I need to use this object in my other controller. Using swing i was doing this in controller constructors and swing construtors. But i dont know how i can do that with JavaFx. Sorry for my bad english. But if u look code, u will understand me.



    Java FX Code:
    package educationManagement;
    
    import educationManagement.view.Main;
    import javafx.application.Application;
    
    public class Driver {
    	
    	public static void main(String[] args) {
    		Application.launch(Main.class, args);
    	}
    }
    Java FX Code:
    [CODE]package educationManagement.view;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
    	@Override
    	public void start(Stage primaryStage) throws Exception {
    		FXMLLoader loader = new FXMLLoader(getClass().getResource("UserLogin.fxml")); //Fxml'in lokasyonunu belirtiyoruz.
    		Parent root = (Parent) loader.load(); //Parent'a cast ediyoruz. Çünkü Scene parent alıyor.
    		 Stage stage = new Stage();            //Pencerimizi oluşturuyoruz.
    		 Scene scene = new Scene(root);		   //Penceremizde çalışacak arayüzü oluşturup içine parentimizi atıyoruz.
    		 stage.setTitle("Kullanıcı Girişi");
    		 stage.setScene(scene);				   //Penceremize arayüzümü ekliyoruz.
    		 stage.show();						   //Penceremizi show methoduyla görünebilir yapıyoruz.
    	}
    
    }
    [/CODE]

    Java FX Code:
    package educationManagement.controller;
    
    import educationManagement.model.DBContext;
    import educationManagement.model.entity.Admin;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Node;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.PasswordField;
    import javafx.scene.control.TextField;
    import javafx.stage.Stage;
    
    public class UserLoginController extends AbstractController {
    
        @FXML
        private Button joinBtn;
        @FXML
        private Button exitBtn;
        @FXML
        private Label statusLabel;
        @FXML
        private TextField userNo;
        @FXML
        private PasswordField password;
        
        
        @FXML
        void buttonEvent(ActionEvent event) {
        	
        	if (joinBtn == event.getSource()) {
        		String userType = userNo.getText().substring(0, 2);
        		switch (userType) {
        		case "10":
        			Admin admin = DBContext.getInstance().admins().isUserExist(Integer.parseInt(userNo.getText()), password.getText());
        			if(admin != null) {
        				final String fxml = "/educationManagement/view/AdminPanel.fxml";
        				try {
        					FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml));
        					Parent root = (Parent) loader.load();
        					AdminController adminController = loader.getController();
        					adminController.setAdmin(admin);
        					Scene scene = new Scene(root);
        					Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
        					stage.setTitle(admin.getFirstName() + " " + admin.getLastName());
        					stage.setScene(scene);
        					stage.show();
        				} catch (Exception e) {
        					e.printStackTrace();
        					System.out.println("");
        				}
        				
        			} else {
        				statusLabel.setText("Kullanıcı Adı veya Şifre Yanlış");
        				userNo.setText("");
        				password.setText("");
        			}
        			break;
        			
        		case "40":
        			
        			break;
        			
        		case "50":
        			
        			break;
    
        		default:
        			statusLabel.setText("Kullanıcı Adı Bulunamadı");
        			break;
        		}
    
    		}
        }
    }
    Java FX Code:
    package educationManagement.controller;
    
    import java.net.URL;
    import java.util.ResourceBundle;
    import educationManagement.model.entity.Admin;
    import educationManagement.model.entity.Student;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Label;
    import javafx.scene.control.TableView;
    
    public class AdminController implements Initializable{
    	
    	private Admin admin;
    
    	@FXML
        private TableView<Student> studentTable;
        
        @FXML
        private Label firstNameLbl;
    	
        public Admin getAdmin() {
    		return admin;
    	}
    	
        public void setAdmin(Admin admin) {
    		this.admin = admin;
    	}
    
        @Override
    	public void initialize(URL location, ResourceBundle resources) {
        	System.out.println(admin);
    		
    	}
       
    }
    Last edited by Tolls; 02-07-2017 at 09:26 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: JavaFx Controller set Object Another Controller

    The initialize method is called when the controller is created (if I remember my ordering in FX correctly), so that happens before you call setAdmin().
    Unless you have somewhere else you are printing the object.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    32
    Rep Power
    0

    Default Re: JavaFx Controller set Object Another Controller

    The initialize method is being called when loading the FXML file. I create another method load. First setting admin and calling this method for what i want to do. This time admin isint null. Thank you @Tolls.

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: JavaFx Controller set Object Another Controller

    Here is another solution:
    1. remove the Initializable Interface from the AdminController Class
    2. replace lines 30-32 with:

    Java FX Code:
    @FXML
        private void initialize() {
            System.out.println(admin);
    and see whether that takes care of it.

  5. #5
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    32
    Rep Power
    0

    Default Re: JavaFx Controller set Object Another Controller

    Quote Originally Posted by benji2505 View Post
    Here is another solution:
    1. remove the Initializable Interface from the AdminController Class
    2. replace lines 30-32 with:

    Java FX Code:
    @FXML
        private void initialize() {
            System.out.println(admin);
    and see whether that takes care of it.
    admin still null. I think still initialize method working with fxmload.

  6. #6
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: JavaFx Controller set Object Another Controller

    Tolls is right, my bad: the controller is instantiated and the initialize method run before you have injected the dependency.

Similar Threads

  1. Replies: 0
    Last Post: 11-30-2015, 01:42 AM
  2. Replies: 14
    Last Post: 05-15-2015, 09:37 AM
  3. How to pass a form one controller to another controller
    By sasikumarkrishnasamy in forum Spring
    Replies: 0
    Last Post: 08-17-2014, 07:48 PM
  4. Need help for controller
    By powerpravin in forum New To Java
    Replies: 0
    Last Post: 04-12-2011, 06:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •