从javafx中的另一个控制器访问控制器的控件

时间:2015-07-12 13:20:49

标签: java javafx

我正在使用加力燃烧室构建一个简单的javafx应用程序。我有一个主窗口,它有一个边框窗格,它的上半部分包含菜单,在中心有一个垂直的分割窗格,它有两个带有fx:id=inputPanefx:id=tablePane的锚窗格。现在在inputPane我正在加载不同的场景并将数据从中提交到tablePane

现在我在inputPane打开了一个有一个按钮的场景,我想将这个场景中的数据提交到表格中,我想将另一个场景打开到inputPane并再次想要将其数据提交到表格中。

adara.fxml

<BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity"  minHeight="180.0" minWidth="-Infinity" prefHeight="507.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.qaf.presentation.adara.AdaraPresenter">
<top>
  <Pane prefHeight="26.0" prefWidth="800.0" BorderPane.alignment="CENTER">
     <children>
        <MenuBar fx:id="menuBar" prefHeight="25.0" prefWidth="800.0">
           <menus>
              <Menu mnemonicParsing="false" text="Stock">
                 <items>
                    <MenuItem fx:id="createStock" mnemonicParsing="false" onAction="#showCreateStockForm" text="Create" />
                    <MenuItem fx:id="incoming" mnemonicParsing="false" onAction="#showIncomingForm" text="Incoming" />
                 </items>
              </Menu>
           </menus>
        </MenuBar>
     </children>
  </Pane>
</top>
 <center>
  <SplitPane dividerPositions="0.5" orientation="VERTICAL" prefHeight="469.0" prefWidth="800.0" BorderPane.alignment="CENTER">
    <items>
        <BorderPane fx:id="contentBorderPane" prefHeight="200.0" prefWidth="200.0">
           <center>
              <AnchorPane fx:id="contentBox" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
           </center>
        </BorderPane>
      <AnchorPane fx:id="tableBox" minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
    </items>
  </SplitPane>
</center>
</BorderPane>

adaraView.java

public class AdaraView extends FXMLView {

}

adaraPresenter.java

public class AdaraPresenter implements Initializable {
    @FXML
    AnchorPane contentBox;
    @FXML
    AnchorPane tableBox;


    StockCreationPresenter stockCreationPresenter;
    InwardsPresenter inwardsPresenter;
    ViewStockPresenter viewStockPresenter;



    @Override
    public void initialize(URL location, ResourceBundle resources) {       
        StockCreationView scView = new StockCreationView();  

        // other codes     
        .
        .


        ViewStockView vsView = new ViewStockView();

        // other codes     
        .
        .

        contentBox.getChildren().add(scView.getView());
        tableBox.getChildren().add(vsView.getView());
    }

    public void showCreateStockForm(){
        StockCreationView scView = new StockCreationView();

        // other codes     
        .
        .
        ViewStockView vsView = new ViewStockView();

        // other codes     
        .
        .

        contentBox.getChildren().add(scView.getView());
        tableBox.getChildren().add(vsView.getView());
    }
    public void showIncomingForm(){
        InwardsView inView = new InwardsView();

        // other codes     
        .
        .

        ViewStockView vsView = new ViewStockView();

        // other codes     
        .
        .

        contentBox.getChildren().add(inView.getView());
        tableBox.getChildren().add(vsView.getView());
    }
}

这是开始阶段和应用的主要类。

Main.java

public class App extends Application
{
    @Override
    public void start(Stage stage) throws Exception {        
        AdaraView appView = new AdaraView();       
        Scene scene = new Scene(appView.getView());    
        stage.setTitle("Adara");
        final String uri = getClass().getResource("app.css").toExternalForm();        
        scene.getStylesheets().add(uri);
        stage.setScene(scene);
        stage.show();
    }

    @Override
    public void stop() throws Exception {        
        Injector.forgetAll();
    }

    public static void main(String[] args) {        
        launch(args);
    }
}

现在这里是InwardsPresenter.java,必须将stockCreationPresenter.java数据保存到表格中,并且必须将内部视图打开到contentPane

InwardsPresenter.java

public class InwardsPresenter implements Initializable {
    @FXML
    Button saveButton;
    @FXML 
    TextField orderNo;
    @FXML 
    TextField other;
    @FXML
    CheckBox save;

    @Inject
    AdaraPresenter adaraPresenter;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }


    public void save() {

        Inwards inward = newInward();

        inward.setOrderNo(Integer.parseInt(orderNo.getText()));
        inward.setOther(other.getText());
        inward.setSave(save.isSelected());

        this.newInward.set(inward);

        adaraPresenter.incomingForm();
    }
} 

//在执行adaraPresenter.incomingForm();时,它显示NPE到contentBox和tableBox。

                 Thank you very much for any help.

0 个答案:

没有答案