2017-12-22 18 views
0

私のコードに詰まっています。 私は多くのコントローラを含むライブラリシステムを書いています。 これは私のメインコントローラ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に正しく設定するにはどうすればよいですか?

答えて

0

あなたはBorrowControllerの間違ったインスタンスを持っている:onCRentActionButton()であなたは、UIの新しいコピーを取得し、再びBorrows.fxmlをロードし、その後、あなたはその新しいコピーに関連付けられているコントローラを取得します。その新しいUIのコピーは表示されないので、あなたは見えないものを更新しています。

代わりに、さまざまなタブのコントローラをメインコントローラに挿入します。 documentationによれば、<fx:include>にはfx:idを、コントローラには"Controller"を付けて注入できます。 MainController中のSO

、追加:あなたはReaderControllerborrowControllerへの参照が必要になりますコントローラをリンクするには

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

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

<fx:include>秒にfx:id秒を追加

public class ReaderController { 

    private BorrowController borrowController ; 

    public void setBorrowController(BorrowController borrowController) { 
     this.borrowController = borrowController ; 
    } 

    // ... 
} 

whあなたは、メインコントローラのinitialize()方法で設定する必要がありICH:

public class MainController { 

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

    // ... 

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

    // ... 
} 

、あなたがMVC approachを使用して、モデルクラスの状態を維持した場合、最終的にあなただけの

@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 { 
     ReaderBorrow rb; 
     int id=CTable.getSelectionModel().getSelectedItem().getId(); 

     rb = SQLController.getInstance().getReaderToBorrow(id); 
     borrowController.setReader(rb); 
    } 
} 

を行うことができ、これらのコントローラ間のすべての結合を避けることができます。

+0

私のmainController.initialize()にnullPointerExceptionがあります。 – Marcin

+0

@Marcinだから何がnullですか? –

+0

ああ、神様、申し訳ありませんが、fxmlファイルにfx:idを追加しませんでした。ありがとう! しかし、fx:idがどのように機能するのか説明できますか?それはそれらを注射のためだけに見えるようにするか? – Marcin

関連する問題