【问题标题】:Why are VBox children not displaying when added dynamically?为什么动态添加时 VBox 子级不显示?
【发布时间】:2020-06-05 02:46:27
【问题描述】:

谁能解释一下为什么这不起作用?

循环(应该)在VBox、ScrollPane、BorderPane(中心位置)内创建一个重复的VBox 模式,每个模式都有从数据库中提取的不同数据。

    public void equipmentPaneBuilder(LinkedList<ModelEquipment> list) {
        LinkedList<ModelEquipment> equipmentList = list;

        for (int i = 0; i < equipmentList.size(); i++) {
            ModelEquipment item = equipmentList.get(i);

            try {
                PaneEquipment equipmentPane = new PaneEquipment();
                equipmentPane.updateFields(item.getTechId(), item.getShortName(), item.getLongDesc()); equipmentPane.setId("equipPane" + i);

                EquipmentPanels.getChildren().add(equipmentPane);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

这是基于VBox 构建的自定义节点,应使用上述方法将其添加到EquipmentPanels VBox。

import javafx.fxml.FXML;
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;

public class PaneEquipment extends VBox {

    @FXML   private TextField techId;
    @FXML   private TextField shortText;
    @FXML   private TextArea longDesc;


    public PaneEquipment() {
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(getClass().getResource("/fxml/PaneEquipment.fxml"));
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        }
        catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }


    public void updateFields(String techIdArg, String shortNameArg, String longDescArg) {

        techID.setText(techIdArg);
        shortText.setText(shortNameArg);
        longDesc.setText(longDescArg);

    }

}

PaneEquipment 对象/节点的 FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<VBox xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0">
         <children>
            <Label maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="40.0" text="Tech ID" />
            <TextField fx:id="techID" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="150.0" promptText="Tech ID Number" />
            <Label layoutX="10.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="92.0" text="Name" />
            <TextField fx:id="shortText" layoutX="50.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="465.0" promptText="Short text description (max. 40 characters)" />
         </children>
      </HBox>
      <HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0">
         <children>
            <Label prefHeight="132.0" prefWidth="65.0" text="Description" wrapText="true" />
            <TextArea fx:id="longDesc" prefHeight="100.0" prefWidth="682.0" promptText="Long text description (max. 300 characters)" wrapText="true" />
         </children>
      </HBox>
   </children>
</VBox>

根据我的测试,一切正常,除了equipmentPane 在程序运行时没有显示,我只是得到一个空白的滚动窗格。数据库和链表工作正常。 updatefields 方法工作正常。不打印任何异常。

如果我用这个替换 equipmentPaneBuiler 循环,它会正确显示:

try {
    VBox vbox = new VBox();
    Label label = new Label();
    vbox.getChildren().add("text");
    EquipmentPanels.getChildren().add(vbox);
    } catch (Exception e) {
        e.printStackTrace();
    }

所以我认为这是我的自定义节点的问题,但我就是想不通。

【问题讨论】:

  • minimal reproducible example ... 请注意 java 命名约定
  • 老实说,我试图做到这一点,但仍然被召唤出来。能否请您举一个具体的例子,因为我一直认为这是正确的,而且显然还差​​得远。
  • 字段名必须以小写开头
  • @kleopatra 好的,谢谢,出于某种原因,我认为 javafx 字段有所不同。我会解决的。
  • 这里是 Java 命名约定的列表:google.github.io/styleguide/javaguide.html#s5-naming

标签: java javafx vbox


【解决方案1】:

您没有 100% 正确实施 自定义组件 方法。

在您的情况下,PaneEquipment 构造函数的执行期间存在 2 个不同的 VBoxes(或 VBox 派生对象)。正在构造的对象和由FXMLLoader 创建的VBox。由FXMLLoader 创建的VBox 包含内容但会被忽略,您只需添加空的PaneEquipment。

要正确实施该方法,您需要使用PaneEquipment 作为根并将fxml 的根元素替换为&lt;fx:root&gt;

<fx:root type="javafx.scene.layout.VBox" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0">
         <children>
            <Label maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="40.0" text="Tech ID" />
            <TextField fx:id="TechID" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="150.0" promptText="Tech ID Number" />
            <Label layoutX="10.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="92.0" text="Name" />
            <TextField fx:id="ShortText" layoutX="50.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="465.0" promptText="Short text description (max. 40 characters)" />
         </children>
      </HBox>
      <HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0">
         <children>
            <Label prefHeight="132.0" prefWidth="65.0" text="Description" wrapText="true" />
            <TextArea fx:id="LongDesc" prefHeight="100.0" prefWidth="682.0" promptText="Long text description (max. 300 characters)" wrapText="true" />
         </children>
      </HBox>
   </children>
</fx:root>
public PaneEquipment() {
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(getClass().getResource("/fxml/PaneEquipment.fxml"));
    fxmlLoader.setController(this);

    fxmlLoader.setRoot(this); // add this line

    try {
        fxmlLoader.load();
    }
    catch (IOException exception) {
        throw new RuntimeException(exception);
    }
}

【讨论】:

  • 完美!我知道它会是那样的小东西!谢谢:D
猜你喜欢
  • 2016-11-08
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多