私のコードに詰まっています。 私は多くのコントローラを含むライブラリシステムを書いています。 これは私のメインコントローラFXMLファイルです:あなたは、すべてのタブが独自のコントローラを持って見ることができるようにJavaFXコントローラのインスタンスを取得するにはどうすればいいですか
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<Pane prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.library.controllers.MainController">
<children>
<VBox>
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="Plik">
<items>
<MenuItem fx:id="Users" mnemonicParsing="false" onAction="#onUsersMenuAction" text="Użytkownicy" />
<MenuItem fx:id="Backup" mnemonicParsing="false" onAction="#onBackupMenuAction" text="Wykonaj kopię zapasową" />
<MenuItem fx:id="CloseProgram" mnemonicParsing="false" onAction="#onCloseProgramMenuAction" text="Zakończ" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Raporty">
<items>
<MenuItem fx:id="ActiveRentedBooks" mnemonicParsing="false" onAction="#onActiveRentedBooksMenuAction" text="Aktywne wypożyczenia" />
<MenuItem fx:id="ActiveBorrowers" mnemonicParsing="false" onAction="#onActiveBorrowersMenuAction" text="Aktualni dłużnicy" />
<MenuItem fx:id="ReportOfRentedBooksPerDay" mnemonicParsing="false" onAction="#onReportOfRentedBooksPerDayMenuAction" text="Raport wypożyczeń/dzień" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Słowniki">
<items>
<MenuItem fx:id="Categories" mnemonicParsing="false" onAction="#onCategoriesMenuAction" text="Kategorie" />
<MenuItem fx:id="UKDDictionary" mnemonicParsing="false" onAction="#onUKDDictionaryMenuAction" text="Słownik UKD" />
<MenuItem fx:id="ReasonsForDefects" mnemonicParsing="false" onAction="#onReasonForDefectsMenuAction" text="Powody ubytków" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Wydruki">
<items>
<MenuItem fx:id="PrintBookLabel" mnemonicParsing="false" onAction="#onPrintBookLabelActionButton" text="Drukuj etykietę książki" />
<MenuItem fx:id="PrintCardOfLoans" mnemonicParsing="false" onAction="#onPrintCardOfLoansMenuAction" text="Drukuj kartę wypożyczająćego" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Ustawienia">
<items>
<MenuItem fx:id="GeneralOptions" mnemonicParsing="false" onAction="#onGeneralOptionsMenuAction" text="Ogólne" />
</items>
</Menu>
</menus>
</MenuBar>
<TabPane prefHeight="547.0" prefWidth="800.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="KBooks" text="Książki">
<fx:include source="Book.fxml" />
</Tab>
<Tab fx:id="CReaders" text="Czytelnicy">
<fx:include source="Readers.fxml" />
</Tab>
<Tab fx:id="WLoans" text="Wypożyczenia">
<fx:include source="Borrows.fxml" />
</Tab>
<Tab fx:id="ZReturns" text="Zwroty" >
<fx:include source="Return.fxml" />
</Tab>
<Tab fx:id="Communication" text="Komunikaty" >
<fx:include source="Comunication.fxml" />
</Tab>
</tabs>
</TabPane>
</children>
</VBox>
</children>
</Pane>
。 私はReaders.fxmlコントローラからBorrows.fxmlコントローラに(別の付属のコントローラに1からのデータを送信したい 私のコード:。 リーダコントローラ
@FXML
void onCRentActionButton(ActionEvent event) throws IOException {
if(CTable.getSelectionModel().getSelectedItem()==null)
{
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Błąd !!!");
alert.setHeaderText("Nie możesz edytować użytkownika, jeśli żadnego"
+ "nie zaznaczyłeś w tabeli!!");
alert.showAndWait();
}
else
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Borrows.fxml"));
loader.load();
logger.info("Kontroller pliku Borrows.fxml to: "+ loader.<BorrowController>getController().getClass());
BorrowController controller = loader.<BorrowController>getController();
ReaderBorrow rb;
int id=CTable.getSelectionModel().getSelectedItem().getId();
rb = SQLController.getInstance().getReaderToBorrow(id);
controller.setReader(rb);
}
BORROWコントローラ
public void setReader(ReaderBorrow rb)
{
logger.info("Ustawianie nazwiska: " + rb.getSurname());
this.WSurname.setText(rb.getSurname());
logger.info("Odczyt wartości: " + WSurname.getText());
}
データが正常に送信されているという良い情報がありますが、WSurname.getText()メソッドは良い情報を返します。 私の見解では、WSurname TextFieldはまだ空です。 私は、ReadersControllerでgetControllerメソッドを呼び出すと、BorrowControllerの別のインスタンスを取得すると思います。 データをTextFieldに正しく設定するにはどうすればよいですか?
私のmainController.initialize()にnullPointerExceptionがあります。 – Marcin
@Marcinだから何がnullですか? –
ああ、神様、申し訳ありませんが、fxmlファイルにfx:idを追加しませんでした。ありがとう! しかし、fx:idがどのように機能するのか説明できますか?それはそれらを注射のためだけに見えるようにするか? – Marcin