2017-02-21 27 views
1

2つのコントローラに通信する際に問題があります。 これらのうちの1つはMainPanelControllerであり、2つ目はAddUserControllerです。 AddUserControllerでUserNameOkButtonをクリックした後、mainControl.getTopPanelController()。getUserSplitMenuButton()で新しい項目を作成します。 しかし、私はこの行に誤りがあります。JavaFX異なるウィンドウで2つのコントローラ間で通信を行う方法

package simplygoals.control; 
import simplygoals.model.*; 
import simplygoals.control.AddUserController; 
import java.util.ArrayList; 
import javafx.scene.control.MenuItem; 
import java.net.URL; 
import java.sql.SQLException; 
import java.util.ResourceBundle; 
import simplygoals.model.MySQLAccess; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.control.SplitMenuButton; 

import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Modality; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

public class MainPanelController extends MainController implements Initializable { 


    @FXML 
    private AddUserController addUserController; 

    @FXML 
    private TopPanelController topPanelController; 

    @FXML 
    private MenuPanelController menuPanelController; 

    @FXML 
    private LeftPanelTimeModeController leftPanelTimeModeController; 

    @FXML 
    private CenterPanelTableController centerPanelTableController; 


    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     //if there is no database create it, otherwise not 

     getAppModel().getMySQL().createDatabase(); 
     getAppModel().copyUsersfromMySqlToAllUsers(); 

     createNewUserTopPanel(); 
     showUsersTopPanel(); 



    } 
    public void showUsersTopPanel(){ 

     SplitMenuButton splitMenuButton = getTopPanelController().getUserSplitMenuButton(); 


         for(String t:getAppModel().getAllUsers().getUsersString()){ 

          final MenuItem menuItem = new MenuItem(t); 
          menuItem.setText(t); 
          splitMenuButton.getItems().add(menuItem); 

         } 

    } 
    public void createNewUserTopPanel(){ 
     MenuItem menuItem = getTopPanelController().getUserSplitMenuItem(); 
     menuItem.setOnAction(new EventHandler<ActionEvent>(){ 

       public void handle(ActionEvent event) { 
          try { 
       FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/simplygoals/view/AddUser.fxml")); 
         Parent root1 = (Parent) fxmlLoader.load(); 
         Stage stage = new Stage(); 
         stage.setScene(new Scene(root1)); 
         stage.show(); 
       } catch(Exception e) { 
        e.printStackTrace(); 
        } 
       } 
     }); 
    } 
    public TopPanelController getTopPanelController() { 
     return topPanelController; 
    } 
} 

第二のコントローラがあります:

package simplygoals.control; 
import java.net.URL; 
import javafx.fxml.Initializable; 
import java.util.ResourceBundle; 
import javafx.fxml.FXML; 
import javafx.scene.control.Button; 
import javafx.scene.control.MenuItem; 
import javafx.scene.control.SplitMenuButton; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.AnchorPane; 
import simplygoals.control.MainPanelController; 
public class AddUserController extends MainController implements Initializable { 

    private MainPanelController mainControl; 

    @FXML 
    private Button UserNameOkButton; 

    @FXML 
    private AnchorPane AddUserAnchor; 

    @FXML 
    private TextField UserNameTextField; 

    @Override 
    public void initialize(URL arg0, ResourceBundle arg1) { 
     UserNameOkButton.setOnAction(x->{ 
      getAppModel().addNewUserToMySql(UserNameTextField.getText()); 
      getAppModel().copyUsersfromMySqlToAllUsers(); 
      SplitMenuButton splitMenuButton = mainControl.getTopPanelController().getUserSplitMenuButton(); 
      splitMenuButton.getItems().clear(); 
      for(String t:getAppModel().getAllUsers().getUsersString()){ 
       final MenuItem menuItem = new MenuItem(t); 
       menuItem.setText(t); 
       splitMenuButton.getItems().add(menuItem); 
      } 
     }); 

    } 

AddUserControllerは新しいウィンドウでコントローラです。そこに私は1つのテキストフィールドとOKボタンがあります。私はこの後、menuplitbuttonの新しい項目をクリックして作成したいのですが、MainPanelControllerを使用する必要があります。 異なるウィンドウで2つのコントローラを通信する方法は? ありがとうございます。ここで エラー:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException 
    at simplygoals.control.AddUserController.lambda$0(AddUserController.java:30) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 
    at javafx.event.Event.fireEvent(Event.java:198) 
    at javafx.scene.Node.fireEvent(Node.java:8411) 
    at javafx.scene.control.Button.fire(Button.java:185) 
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) 
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 
    at javafx.event.Event.fireEvent(Event.java:198) 
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) 
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) 
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) 
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415) 
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555) 
    at com.sun.glass.ui.View.notifyMouse(View.java:937) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    at java.lang.Thread.run(Unknown Source) 

答えて

1

あなたはどこにでもmainControlを初期化していません。あなたがロードするとき

がそれを初期化するためにAddUserControllersetMainControl()方法を定義し、それを呼び出すAddUser.fxml

menuItem.setOnAction(event -> { 
    try { 
     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/simplygoals/view/AddUser.fxml")); 
     Parent root1 = (Parent) fxmlLoader.load(); 
     Stage stage = new Stage(); 
     stage.setScene(new Scene(root1)); 

     AddUserController addUserController = fxmlLoader.getController(); 
     addUserController.setMainControl(this); 

     stage.show(); 
    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
}); 
+0

私はAddUserControllerに追加します。 \t ます。public void setMainControl(MainPanelController mainPanel){ \t \t mainControl = mainPanel ; \t} MainPanelControllerのaddUserController.setMainControl(this); と私は呼び出して、このメソッドの故障している:私は、投稿のように メソッドsetMainControl(MainPanelController)型AddUserControllerでは、引数には適用されません(新しいEventHandlerの(){}) – Kutti

+0

@Kuttiおそらく、あなたはそれを試みることができます匿名の内部クラスの代わりにラムダ式を使用します。 –

+0

@Kutti本当に匿名の内部クラスを使用する必要がある場合(なぜ???)、 'this'の代わりに' MainPanelController.this'を実行してください。 –

関連する問題