前提・実現したいこと
javaFXを学んでいて、いま、スケジュール表を作っています。スケジュール表には、予定を追加したいところをマウスでクリックすると予定を設定できる画面に移り、
そこで設定できるような機能を付けたいので、1つのControllerクラスに2つのFXMLを関連付けてコードを書いていたところ、fx:idを登録したAnchorPane(下のコードのaPane)がnullになっていて、ヌルポが出てしまっていることに気付きました。
2つのfxmlを関連付けるとfx:idはNullになってしまうのでしょうか、教えていただけると助かります。
該当のソースコード
ScheduleTableController
1/** 2 * Sample Skeleton for 'ScheduleIndividual.fxml' Controller Class 3 */ 4 5package application; 6 7 8 9public class ScheduleTableController { 10 11 12 @FXML // ResourceBundle that was given to the FXMLLoader 13 private ResourceBundle resources; 14 15 @FXML // URL location of the FXML file that was given to the FXMLLoader 16 private URL location; 17 18 @FXML // fx:id="pane" 19 private VBox pane; // Value injected by FXMLLoader 20 21 @FXML // fx:id="aPane" 22 private AnchorPane aPane; // Value injected by FXMLLoader 23 24 @FXML // fx:id="aPane" 25 private VBox scheduleIndividual; // Value injected by FXMLLoader 26 27 28 //ここからScheduleSelectのid 29 30 @FXML // fx:id="scheduleName" 31 private TextField scheduleName; // Value injected by FXMLLoader 32 33 @FXML // fx:id="sHour" 34 private ComboBox<String> sHour; // Value injected by FXMLLoader 35 36 @FXML // fx:id="sakusei" 37 private Button sakusei; // Value injected by FXMLLoader 38 39 @FXML // fx:id="packageSelect" 40 private ComboBox<?> packageSelect; // Value injected by FXMLLoader 41 42 @FXML // fx:id="month" 43 private ComboBox<?> month; // Value injected by FXMLLoader 44 45 @FXML // fx:id="scheduleSelct" 46 private AnchorPane scheduleSelect; // Value injected by FXMLLoader 47 48 @FXML // fx:id="year" 49 private ComboBox<String> year; // Value injected by FXMLLoader 50 51 @FXML // fx:id="fHour" 52 private ComboBox<String> fHour; // Value injected by FXMLLoader 53 54 @FXML // fx:id="fMinute" 55 private ComboBox<String> fMinute; // Value injected by FXMLLoader 56 57 @FXML // fx:id="memo" 58 private TextArea memo; // Value injected by FXMLLoader 59 60 @FXML // fx:id="sMinute" 61 private ComboBox<String> sMinute; // Value injected by FXMLLoader 62 63 @FXML // fx:id="day" 64 private ComboBox<String> day; // Value injected by FXMLLoader 65 66 @FXML 67 void clickMemo(MouseEvent event) { 68 69 } 70 71 @FXML 72 void sets(MouseEvent event) { 73 try { 74 showSecondWindow(); 75 } catch (IOException e) { 76 e.printStackTrace(); 77 } 78 } 79 80 @FXML 81 void jikkou(MouseEvent event) { 82 addScheduleLabel(addData()); 83 } 84 85 void showSecondWindow() throws IOException { 86 87 FXMLLoader loader = new FXMLLoader(getClass().getResource("ScheduleSelect.fxml")); 88 AnchorPane root = (AnchorPane) loader.load(); 89 Scene scene = new Scene(root); 90 Stage stage = new Stage(); 91 stage.setScene(scene); 92 stage.showAndWait(); 93 94 } 95 96 ScheduleData addData() { 97 String name = scheduleName.getText(); 98 var sTime = LocalTime.of(Integer.parseInt(sHour.getValue()),Integer.parseInt(sMinute.getValue())); 99 var fTime = LocalTime.of(Integer.parseInt(fHour.getValue()),Integer.parseInt(fMinute.getValue())); 100 String detail = memo.getText(); 101 var sd = new ScheduleData(name, sTime, fTime, detail); 102 return sd; 103 } 104 105 @SuppressWarnings("static-access") 106 void addScheduleLabel(ScheduleData data) { 107 var sLabel = new Label(); 108 double stNum = (data.getsTime().getHour()+data.getsTime().getMinute()/60)*30+4; 109 double ftNum = (data.getfTime().getHour()+data.getfTime().getMinute()/60)*30+4; 110 double tNum = ftNum-stNum; 111 String str = data.gettitle()+"\n"+data.gettime()+"\n"+data.getDetail(); 112 sLabel.setText(str); 113 aPane.getChildren().add(sLabel); 114 aPane.setTopAnchor(sLabel, stNum); 115 sLabel.setPrefHeight(tNum); 116 } 117 118 119 @FXML // This method is called by the FXMLLoader when initialization is complete 120 void initialize() { 121 122 123 124 125 126 assert pane != null : "fx:id=\"pane\" was not injected: check your FXML file 'ScheduleIndividual.fxml'."; 127 128 } 129}
Select
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.text.*?> 4<?import javafx.scene.control.*?> 5<?import java.lang.*?> 6<?import javafx.scene.layout.*?> 7<?import javafx.scene.layout.AnchorPane?> 8 9<AnchorPane fx:id="scheduleSelect" prefHeight="400.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ScheduleTableController"> 10 <children> 11 <Label layoutX="77.0" layoutY="14.0" prefHeight="27.0" prefWidth="127.0" text="予定を追加する" textAlignment="CENTER" AnchorPane.leftAnchor="77.0" AnchorPane.rightAnchor="96.0" AnchorPane.topAnchor="14.0"> 12 <font> 13 <Font size="18.0" /> 14 </font> 15 </Label> 16 <Label layoutX="5.0" layoutY="53.0" prefHeight="20.0" prefWidth="46.0" text="パッケージ" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="53.0" /> 17 <Label layoutX="14.0" layoutY="93.0" text="名前" /> 18 <Label layoutX="14.0" layoutY="133.0" prefHeight="17.0" prefWidth="28.0" text="日付" /> 19 <Label layoutX="14.0" layoutY="172.0" text="時間" /> 20 <Label layoutX="17.0" layoutY="259.0" text="メモ" /> 21 <ComboBox fx:id="packageSelect" editable="true" layoutX="66.0" layoutY="51.0" prefHeight="25.0" prefWidth="121.0" promptText="新規作成の場合は入力" /> 22 <TextField fx:id="scheduleName" layoutX="64.0" layoutY="89.0" prefHeight="25.0" prefWidth="127.0" /> 23 <ComboBox fx:id="year" layoutX="53.0" layoutY="129.0" prefHeight="25.0" prefWidth="50.0" /> 24 <Label layoutX="107.0" layoutY="133.0" prefHeight="17.0" prefWidth="14.0" text="年" /> 25 <Label layoutX="224.0" layoutY="93.0" text="*必須" /> 26 <Label layoutX="248.0" layoutY="160.0" text="*必須" /> 27 <ComboBox fx:id="sMinute" editable="true" layoutX="120.0" layoutY="168.0" prefHeight="25.0" prefWidth="36.0" /> 28 <ComboBox fx:id="sHour" editable="true" layoutX="64.0" layoutY="169.0" prefHeight="25.0" prefWidth="50.0" /> 29 <Label layoutX="114.0" layoutY="161.0" prefHeight="20.0" prefWidth="6.0" text=":"> 30 <font> 31 <Font size="28.0" /> 32 </font> 33 </Label> 34 <Label layoutX="100.0" layoutY="198.0" prefHeight="20.0" prefWidth="20.0" text="~"> 35 <font> 36 <Font size="16.0" /> 37 </font> 38 </Label> 39 <Label layoutX="175.0" layoutY="190.0" prefHeight="20.0" prefWidth="6.0" text=":"> 40 <font> 41 <Font size="28.0" /> 42 </font> 43 </Label> 44 <ComboBox fx:id="fHour" editable="true" layoutX="125.0" layoutY="198.0" prefHeight="25.0" prefWidth="50.0" /> 45 <ComboBox fx:id="fMinute" editable="true" layoutX="181.0" layoutY="197.0" prefHeight="25.0" prefWidth="36.0" /> 46 <TextArea fx:id="memo" layoutX="53.0" layoutY="259.0" onMouseClicked="#clickMemo" prefHeight="91.0" prefWidth="226.0" /> 47 <Button fx:id="sakusei" layoutX="113.0" layoutY="359.0" mnemonicParsing="false" onMouseClicked="#jikkou" prefHeight="27.0" prefWidth="64.0" text="作成" /> 48 <Label layoutX="180.0" layoutY="133.0" prefHeight="17.0" prefWidth="14.0" text="月" /> 49 <ComboBox fx:id="month" layoutX="125.0" layoutY="129.0" prefHeight="25.0" prefWidth="50.0" /> 50 <ComboBox fx:id="day" layoutX="197.0" layoutY="129.0" prefHeight="25.0" prefWidth="50.0" /> 51 <Label layoutX="252.0" layoutY="133.0" prefHeight="17.0" prefWidth="14.0" text="日" /> 52 </children> 53</AnchorPane> 54
Table
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.text.*?> 4<?import javafx.scene.control.*?> 5<?import javafx.geometry.*?> 6<?import java.lang.*?> 7<?import javafx.scene.layout.*?> 8 9<fx:root fx:id="scheduleIndividual" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="130.0" styleClass="ScheduleTable" type="VBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ScheduleTableController"> 10 <children> 11 <AnchorPane prefHeight="100.0" prefWidth="130.0"> 12 <children> 13 <Label text="日付" textAlignment="CENTER" AnchorPane.bottomAnchor="60.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="60.0" AnchorPane.topAnchor="0.0"> 14 <font> 15 <Font name="Monospaced Regular" size="18.0" /> 16 </font> 17 </Label> 18 <CheckBox mnemonicParsing="false" text="CheckBox" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="40.0" /> 19 <CheckBox mnemonicParsing="false" text="CheckBox" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="57.0" /> 20 <CheckBox mnemonicParsing="false" text="CheckBox" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="74.0" /> 21 </children> 22 </AnchorPane> 23 <ScrollPane prefHeight="400.0" prefWidth="130.0" vbarPolicy="ALWAYS"> 24 <content> 25 <HBox minWidth="130.0" prefHeight="740.0" prefWidth="130.0"> 26 <children> 27 <AnchorPane prefHeight="740.0" prefWidth="30.0"> 28 <children> 29 <Label text="0:00" AnchorPane.topAnchor="0.0" /> 30 </children> 31 </AnchorPane> 32 <AnchorPane fx:id="aPane" onMouseClicked="#sets" prefHeight="740.0" prefWidth="100.0" /> 33 </children> 34 </HBox> 35 </content> 36 </ScrollPane> 37 <TextArea prefHeight="100.0" prefWidth="130.0" text="感想" /> 38 </children> 39</fx:root> 40
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。