###前提のソースコード
java
1// Main.java 2import javafx.application.Application; 3import javafx.fxml.FXMLLoader; 4import javafx.scene.Parent; 5import javafx.scene.Scene; 6import javafx.stage.Stage; 7 8public class Main extends Application { 9 10 @Override 11 public void start(Stage primaryStage) throws Exception{ 12 Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 13 primaryStage.setTitle("Hello World"); 14 primaryStage.setScene(new Scene(root, 300, 275)); 15 primaryStage.show(); 16 } 17 18 public static void main(String[] args) { 19 launch(args); 20 } 21} 22 23// Pane1Controller.java 24import javafx.event.ActionEvent; 25import javafx.fxml.FXML; 26import javafx.scene.control.Button; 27import javafx.scene.control.Label; 28 29public class Pane1Controller { 30 @FXML 31 private Label label; 32 @FXML 33 private Button button; 34 35 private SampleController sampleController; 36 37 public SampleController getSampleController() { 38 return sampleController; 39 } 40 41 public void setSampleController(SampleController sampleController) { 42 this.sampleController = sampleController; 43 } 44 45 @FXML 46 private void onButton(ActionEvent event) { 47 label.setText(sampleController.getPane2Controller().getTextField().getText()); 48 } 49} 50 51// Pane2Controller.java 52import javafx.fxml.FXML; 53import javafx.scene.control.TextField; 54 55public class Pane2Controller { 56 @FXML 57 private TextField textField; 58 59 private SampleController sampleController; 60 61 public SampleController getSampleController() { 62 return sampleController; 63 } 64 65 public void setSampleController(SampleController sampleController) { 66 this.sampleController = sampleController; 67 } 68 69 public TextField getTextField() { 70 return textField; 71 } 72} 73 74// SampleController.java 75import javafx.fxml.FXML; 76import javafx.fxml.Initializable; 77 78import java.net.URL; 79import java.util.ResourceBundle; 80 81public class SampleController implements Initializable { 82 @FXML 83 private Pane1Controller pane1Controller; 84 85 @FXML 86 private Pane2Controller pane2Controller; 87 88 @Override 89 public void initialize(URL location, ResourceBundle resources) { 90 pane1Controller.setSampleController(this); 91 pane2Controller.setSampleController(this); 92 } 93 94 public Pane1Controller getPane1Controller() { 95 return pane1Controller; 96 } 97 98 public Pane2Controller getPane2Controller() { 99 return pane2Controller; 100 } 101}
xml
1<!-- pane1.fxml --> 2<?xml version="1.0" encoding="UTF-8"?> 3 4<?import javafx.scene.control.Button?> 5<?import javafx.scene.control.Label?> 6<?import javafx.scene.layout.HBox?> 7<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" 8 prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" 9 fx:controller="Pane1Controller"> 10 <children> 11 <Label fx:id="label"/> 12 <Button fx:id="button" mnemonicParsing="false" onAction="#onButton" text="Button"/> 13 </children> 14</HBox> 15 16 17<!-- pane2.fxml --> 18<?xml version="1.0" encoding="UTF-8"?> 19 20<?import javafx.scene.control.TextField?> 21<?import javafx.scene.layout.Pane?> 22<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" 23 prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" 24 fx:controller="Pane2Controller"> 25 <children> 26 <TextField fx:id="textField"/> 27 </children> 28</Pane> 29 30<!-- sample.fxml --> 31<?xml version="1.0" encoding="UTF-8"?> 32 33<?import javafx.scene.control.Tab?> 34<?import javafx.scene.control.TabPane?> 35<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" 36 prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns:fx="http://javafx.com/fxml/1" 37 xmlns="http://javafx.com/javafx/8.0.111" 38 fx:controller="SampleController"> 39 <tabs> 40 <Tab text="Pane1"> 41 <content> 42 <fx:include source="pane1.fxml" fx:id="pane1"/> 43 </content> 44 </Tab> 45 <Tab text="Pane2"> 46 <content> 47 <fx:include source="pane2.fxml" fx:id="pane2"/> 48 </content> 49 </Tab> 50 </tabs> 51</TabPane> 52
上記ソースでPane1ControllerからPane2Controllerを参照する上記以外の良い, 汎用性のある書き方は無いでしょうか?
動作環境: JDK8 - 111
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。