有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在使用afterburner构建一个简单的javafx应用程序。我有一个主窗口,它有一个边框窗格,它的上部包含菜单,中间有一个垂直拆分窗格,它有两个锚定窗格,分别是fx:id=inputPanefx:id=tablePane。现在在inputPane中,我正在加载不同的场景,并将其中的数据提交到tablePane

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

阿达拉。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>

阿达拉维。爪哇

public class AdaraView extends FXMLView {

}

阿达拉帕内特。爪哇

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());
    }
}

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

梅因。爪哇

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的内部视图

内心中心。爪哇

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();时,它向contentBox和tableBox显示NPE

                 Thank you very much for any help.

共 (0) 个答案