JavaFX的。控制器之间的通信

时间:2014-10-27 14:48:28

标签: javafx tabs

您好我是从JavaFX开始的,我遇到了一些问题。 我想从两个不同的视图中传达两个控件。我该怎么做? 我正在使用标签,我有两个控制器,我想做类似的事情:

Application.java:

public class JavaFXApplication6 extends Application 
{

@Override
public void start(Stage primaryStage) 
{
    try 
    {
        Parent root = FXMLLoader.load(getClass().getResource(
                "/view/FXML_Main.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e){
        e.printStackTrace();
    }

    primaryStage.setTitle("Back-end GUI");
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{
    launch(args);
}

}

FXML_Main.fxml:     

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" 
    xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" 
    fx:controller="controller.FXML_MainController">
   <children>
      <TabPane layoutX="61.0" layoutY="30.0" prefHeight="400.0" prefWidth="600.0" 
         tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" 
         AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" 
         AnchorPane.topAnchor="0.0">
        <tabs>
          <Tab text="Untitled Tab 1">
               <content>
                  <fx:include source="FXML_Tab1.fxml" />
               </content>
          </Tab>
          <Tab text="Untitled Tab 2">
               <content>
                  <fx:include source="FXML_Tab2.fxml" />
               </content>
          </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>

FXML_MainController.java:

public class FXML_MainController implements Initializable {

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

}

FXML_Tab1.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0"
     xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
     fx:controller="controller.FXML_Tab1Controller">
<children>
  <Label fx:id="Label1" layoutX="282.0" layoutY="108.0" text="Label" />
  <TextField fx:id="FextField1" layoutX="215.0" layoutY="146.0" />
  <Button fx:id="Button1" layoutX="269.0" layoutY="197.0" mnemonicParsing="false"
      onAction="#actionButton1" text="Button" />
</children>
</AnchorPane>

FXML_Tab1Controller.java:

public class FXML_Tab1Controller implements Initializable {

FXML_Tab2Controller tab2controller;

@FXML public Label Label1;
@FXML public TextField TextField1;
@FXML public Button Button1;
/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
} 

@FXML private void actionButton1(ActionEvent event)
{
    Label1.setText(tab2controller.TextField2.getText());
}

}

FXML_Tab2.fxml:         

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" 
    xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
    fx:controller="controller.FXML_Tab2Controller">
   <children>
      <Label fx:id="Label2" layoutX="282.0" layoutY="99.0" text="Label" />
      <TextField fx:id="TextField2" layoutX="215.0" layoutY="149.0" />
      <Button fx:id="Button2" layoutX="270.0" layoutY="200.0" mnemonicParsing="false" 
    onAction="#actionButton2" text="Button" />
   </children>
</AnchorPane>

FXML_Tab2Controller.java:

public class FXML_Tab2Controller implements Initializable {

FXML_Tab1Controller tab1controller;
@FXML public Label Label2;
@FXML public TextField TextField2;
@FXML public Button Button2;
/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
} 

@FXML private void actionButton2(ActionEvent event){
        Label2.setText(tab1controller.TextField1.getText());
}

}
像那个视频类似的东西: https://www.youtube.com/watch?v=XLVx46ycxco

1 个答案:

答案 0 :(得分:4)

为每个fx:id代码添加<fx:include>

    <tabs>
      <Tab text="Untitled Tab 1">
           <content>
              <fx:include source="FXML_Tab1.fxml" fx:id="tab1" />
           </content>
      </Tab>
      <Tab text="Untitled Tab 2">
           <content>
              <fx:include source="FXML_Tab2.fxml" fx:id="tab2" />
           </content>
      </Tab>
    </tabs>

这样您就可以将相应的控制器注入FXML_MainController

public class FXML_MainController  {

    @FXML
    private FXML_Tab1Controller tab1Controller ;
    @FXML
    private FXML_Tab2Controller tab2Controller ;

}

变量命名在这里非常重要:字段必须命名为xController,其中xfx:idfx:include属性的值。有关详细信息,请参阅Introduction to FXML documentation

现在在主控制器的initialize()方法中,您可以建立两个控制器之间的关系:

public class FXML_MainController  {

    @FXML
    private FXML_Tab1Controller tab1Controller ;
    @FXML
    private FXML_Tab2Controller tab2Controller ;

    public void initialize() {
        tab1Controller.tab2Controller = tab2Controller ;
        tab2Controller.tab1Controller = tab1Controller ;
    }
}