Я в процессе создания настольного приложения javafx. Проблема, с которой я сталкиваюсь, заключается в том, что если я добавляю контроллер в свои файлы fxml, элементы управления каким-то образом исчезают. Программа использует плавный переход для переключения между файлами fxml.
AnchorPane reorderLevels,purchaseorder,onlineSales,generalLedger
,cashBook,payments,departmentalTransfers
,purchaseInvoice
,productMantainance,
branchTransfers,stockMovement,invoiceRegistration,stockTake;
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@Override
public void initialize(URL url, ResourceBundle rb) {
//load all the fxml screens when the buttons are clicked
try {
//screens for the application
reorderLevels = FXMLLoader.load(getClass().getResource("ReorderLevels.fxml"));
purchaseorder = FXMLLoader.load(getClass().getResource("PurchaseOrder.fxml"));
onlineSales = FXMLLoader.load(getClass().getResource("OnlineSales.fxml"));
generalLedger = FXMLLoader.load(getClass().getResource("GeneralLedger.fxml"));
cashBook = FXMLLoader.load(getClass().getResource("CashBook.fxml"));
departmentalTransfers = FXMLLoader.load(getClass().getResource("DepartmentalTransfers.fxml"));
purchaseInvoice = FXMLLoader.load(getClass().getResource("PurchaseInvoice.fxml"));
productMantainance = FXMLLoader.load(getClass().getResource("ProductMantainance.fxml"));
branchTransfers = FXMLLoader.load(getClass().getResource("BranchTransfers.fxml"));
stockMovement = FXMLLoader.load(getClass().getResource("StockMovement.fxml"));
payments = FXMLLoader.load(getClass().getResource("Payments.fxml"));
invoiceRegistration = FXMLLoader.load(getClass().getResource("InvoiceRegistration.fxml"));
stockTake = FXMLLoader.load(getClass().getResource("StockTake.fxml"));
//set each and every individual node when the buttons are clicked
setNode(reorderLevels);
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void setNode(Node node) {
holderPane.getChildren().clear();
holderPane.getChildren().add((Node) node);
//the transition between the screens
FadeTransition ft = new FadeTransition(Duration.millis(1));
ft.setNode(node);
ft.setFromValue(0.1);
ft.setToValue(1);
ft.setCycleCount(1);
ft.setAutoReverse(false);
ft.play();
}
/* screens */
@FXML
private void switchReorderLevels(ActionEvent event) {
setNode(reorderLevels);
}
@FXML
private void switchOnlineSales(ActionEvent event) {
setNode(onlineSales);
}
@FXML
private void switchGeneralLedger(ActionEvent event) {
setNode(generalLedger);
}
@FXML
private void switchPurchaseOrder(ActionEvent event) {
setNode(purchaseorder);
}
@FXML
private void switchCashBook(ActionEvent event) {
setNode(cashBook);
}
@FXML
private void switchAlert(ActionEvent event) {
setNode(purchaseorder);
}
@FXML
private void switchPayments(ActionEvent event) {
setNode(payments);
}
@FXML
private void switchDepartmentalTransfers(ActionEvent event) {
setNode(departmentalTransfers);
}
@FXML
private void switchPurchaseInvoice(ActionEvent event) {
setNode(purchaseInvoice);
}
@FXML
private void switchProductMantainance(ActionEvent event) {
setNode(productMantainance);
}
@FXML
private void switchBranchTransfers(ActionEvent event) {
setNode(branchTransfers);
}
//this is a screen that will displayone of the layouts that will show re order levels
<AnchorPane id = "AnchorPane" prefHeight = "713.0" prefWidth = "1219.0" style = "-fx-background-color: white;" xmlns = "http://javafx.com/javafx/9.0.1" xmlns:fx = "http://javafx.com/fxml/1">
<children>
<Label layoutX = "24.0" layoutY = "17.0" prefHeight = "30.0" prefWidth = "285.0" style = "-fx-font-size: 20; -fx-font-weight: bold; -fx-text-fill: #7387A8;" text = "ReOrder Levels" />
<Separator layoutX = "25.0" layoutY = "54.0" prefHeight = "5.0" prefWidth = "1191.0" />
<Pane fx:id = "tbl_reorderLevels" layoutX = "25.0" layoutY = "108.0" prefHeight = "413.0" prefWidth = "1174.0" style = "-fx-background-color: #EFEFEF;">
<children>
<JFXComboBox fx:id = "cmd_reOrder_productCode" layoutX = "48.0" layoutY = "33.0" prefHeight = "25.0" prefWidth = "267.0" promptText = "Product Code" />
<JFXTextField fx:id = "txt_reOrder_reorderLevel" labelFloat = "true" layoutX = "48.0" layoutY = "91.0" prefHeight = "25.0" prefWidth = "267.0" promptText = "Reorder Level" />
// это корневой макет, в котором есть кнопки, которые будут перемещаться между экранами с помощью плавного перехода
<?xml version = "1.0" encoding = "UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<!-- root layout -->
<AnchorPane id = "AnchorPane" nodeOrientation = "LEFT_TO_RIGHT" prefHeight = "623.0" prefWidth = "1271.0" style = "-fx-background-color: #fff;" xmlns = "http://javafx.com/javafx/9.0.1" xmlns:fx = "http://javafx.com/fxml/1" fx:controller = "dashboard.FXMLDocumentController">
<children>
<Pane layoutX = "-2.0" prefHeight = "81.0" prefWidth = "1272.0" style = "-fx-background-color: #0b8dee;" styleClass = "head-background" stylesheets = "@style.css" AnchorPane.leftAnchor = "0.0" AnchorPane.rightAnchor = "0.0" AnchorPane.topAnchor = "0.0">
<children>
<Label fx:id = "labelEstock" layoutX = "204.0" layoutY = "-3.0" prefHeight = "88.0" prefWidth = "536.0" style = "-fx-shape: round;" stylesheets = "@custom.css" text = "Oasys Estock" textFill = "WHITE">
<font>
<Font name = "Brush Script MT Italic" size = "36.0" />
</font>
</Label>
<!-- <ImageView fx:id = "imageEstock" fitHeight = "110.0" fitWidth = "92.0" layoutX = "102.0" layoutY = "8.0" pickOnBounds = "true" preserveRatio = "true">
<image>
<Image
url = "@../../../../AndroidStudioProjects/OasysEstock/app/src/main/res/drawable/fortified_icon.png" />
</image>
</ImageView> -->
</children>
</Pane>
<VBox layoutY = "85.0" maxHeight = "-Infinity" maxWidth = "-Infinity" minHeight = "-Infinity" minWidth = "-Infinity" prefHeight = "518.0" prefWidth = "122.0" style = "-fx-background-color: #0b8dee; -fx-background-radius: 15;">
<children>
<JFXButton id = "btnReorderLevels" fx:id = "btnReorderLevels" buttonType = "RAISED" maxWidth = "-Infinity" minHeight = "-Infinity" minWidth = "-Infinity" onAction = "#switchReorderLevels" prefHeight = "47.0" prefWidth = "107.0" style = "-fx-background-radius: 100;" text = "ReOrder Levels" textFill = "#f5f0f0">
<VBox.margin>
<Insets />
</VBox.margin>
<font>
<Font name = "Calibri" size = "12.0" />
</font>
</JFXButton>
<JFXButton fx:id = "btnInvoiceRegistration" onAction = "#switchInvoiceRegistration" prefHeight = "25.0" prefWidth = "121.0" text = "Invoice Registration" textFill = "#f8f4f4" />
<JFXButton fx:id = "btnPurchaseOrder" buttonType = "RAISED" onAction = "#switchPurchaseOrder" prefHeight = "34.0" prefWidth = "107.0" text = "Purchase Order" textFill = "#f5f0f0">
<font>
<Font name = "Cambria" size = "12.0" />
</font>
</JFXButton>
<JFXButton fx:id = "btnOnlineSales" onAction = "#switchOnlineSales" prefHeight = "39.0" prefWidth = "107.0" text = "Online Sales" textFill = "#fff9f9">
<font>
<Font name = "Calibri" size = "12.0" />
</font>
</JFXButton>
<JFXButton fx:id = "btnGeneralLedger" onAction = "#switchGeneralLedger" prefHeight = "40.0" prefWidth = "108.0" text = "General Ledger" textFill = "#f2eeee" />
<JFXButton fx:id = "btnCashBook" onAction = "#switchCashBook" prefHeight = "35.0" prefWidth = "104.0" text = "Cash Book" textFill = "#fffefe" />
<JFXButton fx:id = "btnPayments" buttonType = "RAISED" onAction = "#switchPayments" prefHeight = "25.0" prefWidth = "114.0" text = "Payments" textFill = "#fcf6f6" />
<JFXButton fx:id = "btnDepartmentalTransfers" buttonType = "RAISED" layoutX = "10.0" layoutY = "205.0" onAction = "#switchDepartmentalTransfers" prefHeight = "25.0" prefWidth = "114.0" text = "Dept Transfers" textFill = "#fcf6f6" />
<JFXButton fx:id = "btnProductMantainance" buttonType = "RAISED" layoutX = "10.0" layoutY = "230.0" onAction = "#switchProductMantainance" prefHeight = "56.0" prefWidth = "121.0" text = "Product Mantainance" textFill = "#fcf6f6">
<font>
<Font size = "11.0" />
</font>
</JFXButton>
<JFXButton fx:id = "btnPurchaseInvoice" buttonType = "RAISED" layoutX = "10.0" layoutY = "255.0" onAction = "#switchPurchaseInvoice" prefHeight = "56.0" prefWidth = "121.0" text = "Purchase Invoice" textFill = "#fcf6f6">
<font>
<Font size = "11.0" />
</font>
</JFXButton>
<JFXButton fx:id = "btnBranchTransfers" buttonType = "RAISED" layoutX = "10.0" layoutY = "311.0" onAction = "#switchBranchTransfers" prefHeight = "56.0" prefWidth = "121.0" text = "Branch Transfers" textFill = "#fcf6f6">
<font>
<Font size = "11.0" />
</font>
</JFXButton>
<JFXButton fx:id = "btnStockMovement" buttonType = "RAISED" layoutX = "10.0" layoutY = "367.0" onAction = "#switchStockMovement" prefHeight = "56.0" prefWidth = "121.0" text = "Stock Movement" textFill = "#fcf6f6">
<font>
<Font size = "11.0" />
</font>
</JFXButton>
<JFXButton fx:id = "btnStockTake" buttonType = "RAISED" layoutX = "10.0" layoutY = "448.0" onAction = "#switchStockTake" prefHeight = "56.0" prefWidth = "121.0" text = "Stock Take" textFill = "#fcf6f6">
<font>
<Font size = "11.0" />
</font>
</JFXButton>
</children>
<effect>
<InnerShadow blurType = "TWO_PASS_BOX" choke = "0.58" color = "#212122" height = "26.69" radius = "10.620000000000001" width = "17.79" />
</effect>
</VBox>
<AnchorPane fx:id = "holderPane" layoutX = "125.0" layoutY = "96.0" prefHeight = "507.0" prefWidth = "1134.0"/>
</children>
</AnchorPane>
хорошо, элементы управления являются дочерними элементами панели управления. вот структура одного из экранов, который будет отображаться.
Означает ли "добавить новый контроллер" вызов setNode() из различных событий onAction? Что именно исчезает? Означает ли это, что конечным результатом будет сцена, на которой ничего не видно?
да, если я помещаю onAction в fxml, тогда сцена ничего не отображает. Или, если я сделаю это в контроллере, произойдет то же самое. И если я добавлю контроллер на панель привязки, на сцене также ничего не отобразится.
Примечание: Pane и AnchorPane без указания якорей - худшие макеты, когда дело доходит до адаптивного макета (в этом случае у вас есть только абсолютные позиции ...). В указании layoutX и layoutY для дочерних элементов VBox нет необходимости, поскольку эти значения в любом случае будут установлены на значения, вычисленные во время первого прохода макета; эти рассчитанные значения не принимают во внимание предыдущие значения layoutX / layoutY.
хорошо отметил большое спасибо за указание на это. так есть ли способ исправить это
изменил способ, которым я это реализовал. Огромное спасибо





Возможно, эти элементы управления также являются потомками
holderPane? Если нет, опишите, что именно происходит, и добавьте структуру сцены (= минимальный fxml, позволяющий воспроизвести проблему).