我如何获得JavaFX控制器的实例

时间:2017-12-22 16:24:14

标签: javafx textfield

我被困在我的代码中。 我正在写一个包含许多控制器的库系统。 这是我的主控制器fxml文件:

<?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控制器)。 我的代码: 读者控制器

    @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 CONTROLLER

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正确?

1 个答案:

答案 0 :(得分:0)

您的BorrowController实例错误:在onCRentActionButton()中再次加载Borrows.fxml ,获取用户界面的新副本,然后获得与该新副本相关联的控制器。由于永远不会显示该UI的新副本,因此您正在更新您看不到的内容。

而是将不同选项卡的控制器注入主控制器。根据{{​​3}},您可以使用附加fx:id的{​​{1}} <fx:include>来注入控制器。

所以在"Controller"中,添加:

MainController

@FXML private BorrowController borrowController ; @FXML private ReaderController readerController ; 添加到fx:id s:

<fx:include>

要关联控制器,您需要 <Tab fx:id="CReaders" text="Czytelnicy"> <fx:include fx:id="reader" source="Readers.fxml" /> </Tab> <Tab fx:id="WLoans" text="Wypożyczenia"> <fx:include fx:id="borrow" source="Borrows.fxml" /> </Tab> borrowController的引用:

ReaderController

您需要在主控制器的public class ReaderController { private BorrowController borrowController ; public void setBorrowController(BorrowController borrowController) { this.borrowController = borrowController ; } // ... } 方法中进行设置:

initialize()

然后最后你可以做到

public class MainController {

    @FXML
    private BorrowController borrowController ;
    @FXML
    private ReaderController readerController ;

    // ...

    public void initialize() {
        readerController.setBorrowController(borrowController);
        // ...
    }

    // ...
}

如果使用documentation并将状态保留在模型类中,则可以避免这些控制器之间的所有耦合。