0
我有兩個帶控制器的* .fxml表單。首先是Window,其次是ProductPane。JavaFX TabPane選擇無法正常工作
簡化Window.fxml是:
<BorderPane prefWidth="650.0" prefHeight="450.0" fx:controller="package.WindowController">
<center>
<TabPane fx:id="tabsPane">
<tabs>
<Tab fx:id="productsTab" text="Products"/>
<Tab fx:id="warehouseTab" text="Warehouse"/>
<Tab fx:id="saleTab" text="Sale"/>
</tabs>
</TabPane>
</center>
</BorderPane>
控制器Window.fxml:
public class WindowController {
@FXML
private TabPane tabsPane;
@FXML
private Tab productsTab;
@FXML
void initialize() {
sout("Main Window initialization...");
tabsPane.getSelectionModel().selectedIndexProperty().addListener((e, o, n) -> {
sout("Changed to " + n);
});
tabsPane.getSelectionModel().selectedItemProperty().addListener((e, o, n) -> {
sout("New item: " + n);
// Load ProductPane content:
if(n == productsTab) {
try {
Parent p = FXMLLoader.load(getClass().getResource("productPane.fxml"));
n.setContent(p);
} catch(IOException ex) {
ex.printStackTrace();
}
}
});
sout("Select first item...");
tabsPane.getSelectionModel().selectFirst();
// This methods also don't work
// tabsPane.getSelectionModel().clearAndSelect();
// tabsPane.getSelectionModel().select(productTab);
// tabsPane.getSelectionModel().select(0);
}
}
的問題是:當我加載Window.fxml在main()並運行,appearse窗口空的第一個標籤。
調試輸出:
Main Window initialization...
Select first item...
但ProductPane沒有加載和聽衆不叫。如果我在Window中的選項卡之間切換,則會觸發偵聽器並正確加載產品選項卡。
有什麼問題?
感謝您的解釋。這是個好主意。但是我已經使用了另一種方法:直接在Window.fxml中包含ProductPane.fxml並使用嵌入式窗格和控制器。本文有助於:[Oracle文檔](https://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm) – Ivan