当前位置: 首页 > 面试题库 >

JavaFX:ListView不显示在GUI中

茹正初
2023-03-14
问题内容

我的问题是我的ListView没有在GUI上显示任何内容,我添加了一个占位符进行测试,它的确显示了占位符文本,但除此之外我无法添加到列表中。

我的目标是要列出我所有客户帐户的列表,我已经包含了Account类,我想在ListView中显示所有帐户ID。

由于某些原因,它不起作用,我尝试仅输入随机数据只是为了查看是否是问题所在,但仍然不走运。

我的Main.class文件中有一个名为allAccounts的静态Set。

非常感谢您的帮助,我已经在近处进行了搜索,并且由于我不了解为什么这是行不通的,所以我很努力。

控制器类

public class Controller implements Initializable{

    @FXML
    private TableView<?> tableView;
    @FXML
    private TableColumn<?, ?> tableAccount;
    @FXML
    private TableColumn<?, ?> tableDate;
    @FXML
    private TableColumn<?, ?> tableTime;
    @FXML
    private TableColumn<?, ?> tableTotal;
    @FXML
    private TableColumn<?, ?> tableNotes;
    @FXML
    private ListView<String> list;
    @FXML
    private TextArea invoiceView;
    @FXML
    private TextField amountField;
    @FXML
    private Button addAmount;
    @FXML
    private Button newInvoice;
    @FXML
    private Button saveInvoice;
    @FXML
    private Button deleteInvoice;
    @FXML
    private Label messageBar;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //list.setPlaceholder(new Label("No Content In List"));
        ObservableList<String> names = FXCollections.observableArrayList(
                "Julia", "Ian", "Sue", "Matthew", "Hannah", "Stephan", "Denise");
        list = new ListView<>();
        list.setItems(names);

        //tableView = new TableView<>(names);

    }

    @FXML
    public void newInvoiceFired() {
        System.out.println("new invoice button has been clicked");
    }

    @FXML
    public void deleteInvoiceFired() {
        System.out.println("delete invoice button has been clicked");

    }
    @FXML
    public void saveInvoiceFired() {
        System.out.println("save invoice button has been clicked");

    }

    public void setAccountsList(){

        for (Account account : Main.getAllAccounts()){

        }

    }

}

GUI.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="477.0" prefWidth="641.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
   <children>
      <SplitPane dividerPositions="0.24249999999999972" layoutY="50.0" prefHeight="427.0" prefWidth="641.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="50.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <ListView fx:id="list" layoutX="-24.0" layoutY="62.0" prefHeight="425.0" prefWidth="151.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="445.0" prefWidth="411.0">
               <children>
                  <SplitPane dividerPositions="0.30023640661938533" layoutX="143.0" layoutY="123.0" orientation="VERTICAL" prefHeight="445.0" prefWidth="480.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <items>
                      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
                           <children>
                              <TableView layoutX="128.0" layoutY="45.0" prefHeight="208.0" prefWidth="478.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                                <columns>
                                    <TableColumn fx:id="tableAccount" prefWidth="75.0" text="Account" />
                                  <TableColumn fx:id="tableDate" prefWidth="75.0" text="Date" />
                                  <TableColumn fx:id="tableTime" prefWidth="75.0" text="Time" />
                                    <TableColumn fx:id="tableTotal" prefWidth="75.0" text="Amount" />
                                    <TableColumn fx:id="tableNotes" prefWidth="75.0" text="Notes" />
                                </columns>
                              </TableView>
                           </children>
                        </AnchorPane>
                      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="286.0" prefWidth="478.0">
                           <children>
                              <SplitPane dividerPositions="0.4474789915966387" layoutX="168.0" layoutY="48.0" prefHeight="292.0" prefWidth="478.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                                <items>
                                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                                       <children>
                                          <TextArea fx:id="invoiceView" layoutX="5.0" layoutY="14.0" prefHeight="290.0" prefWidth="209.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                                       </children>
                                    </AnchorPane>
                                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                                       <children>
                                          <TextField fx:id="amountField" layoutX="82.0" layoutY="86.0" prefHeight="27.0" prefWidth="104.0" promptText="0.00" />
                                          <Button fx:id="addAmount" layoutX="195.0" layoutY="86.0" mnemonicParsing="false" text="Add" />
                                          <Label layoutX="14.0" layoutY="91.0" text="Amount: : " />
                                       </children>
                                    </AnchorPane>
                                </items>
                              </SplitPane>
                           </children>
                        </AnchorPane>
                    </items>
                  </SplitPane>
               </children>
            </AnchorPane>
        </items>
      </SplitPane>
      <ImageView fitHeight="150.0" fitWidth="200.0" layoutX="14.0" layoutY="11.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@Icon/Logo.png" />
         </image>
      </ImageView>
      <HBox alignment="CENTER_RIGHT" layoutX="462.0" layoutY="14.0" spacing="15.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
         <children>
            <Button fx:id="newInvoice" mnemonicParsing="false" onAction="#newInvoiceFired" text="New" />
            <Button fx:id="saveInvoice" mnemonicParsing="false" onAction="#deleteInvoiceFired" text="Save" />
            <Button fx:id="deleteInvoice" mnemonicParsing="false" onAction="#deleteInvoiceFired" text="Delete" />
         </children>
      </HBox>
   </children>
</AnchorPane>

帐户类

public class Account {
    private String id;
    private String name;
    private String address1;
    private String address2;
    private String postCode;
    private Set<Invoice> invoiceArchive = new HashSet<>();

    public Account(String id, String name, String address1, String address2, String postCode) {
        this.id = id;
        this.name = name;
        this.address1 = address1;
        this.address2 = address2;
        this.postCode = postCode;
        Main.addAccount(this);
    }

    public Account(String id, String name, String address1, String postCode) {
        this.id = id;
        this.name = name;
        this.address1 = address1;
        this.postCode = postCode;
    }

    public Account(String name, String id) {
        this.name = name;
        this.id = id;
    }

    public String getId() {

        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public String getPostCode() {
        return postCode;
    }

    public void setPostCode(String postCode) {
        this.postCode = postCode;
    }

    public Set<Invoice> getInvoiceArchive() {
        return invoiceArchive;
    }

    public Invoice newInvoice(){
        Invoice i = new Invoice(this);
        invoiceArchive.add(i);
        return i;
    }

    @Override
    public String toString(){
        return this.getId();
    }
}

问题答案:

您将创建listin
的新实例Controller,尽管您的listview是在fxml文件中初始化的。然后,您将项目添加到新实例,但没有将其添加到场景图,因此原始listView保持为空



 类似资料:
  • 大家好,我是JavaFXFXML的初学者,我正在用FXMLTable view进行测试,但是netbeans在我的代码中没有显示任何错误,我无法运行我的GUI应用程序。 这里是我的fxmldocument.fxml代码。 这是一个fxml控制器代码。 这里是Person类代码。(Person.java) 这是主要的table.java代码。 当我运行我的项目时,GUI没有出现,它显示了许多类似的错

  • 我的工作在窗口和视觉工作室和我的目标是尝试定义一个语法来识别一些短语的部分我遵循这个指南https://theantlrguy.atlassian.net/wiki/display/ANTLR4/Getting开始与ANTLR v4 一封又一封,当我 grun你好r-gui cmd没有显示任何内容,它没有显示任何类型的错误我该如何解决这个问题?谢谢大家!!! PS指南告诉添加antlr-4.5-c

  • 我有一个Java swing GUI,使用其他窗口管理器可以很好地运行和显示,但当我在Xmonad中运行它时,它不能正确显示。所有显示的是窗口的框架,它是灰色的,没有按钮,菜单等。我如何使Swing GUI显示正确?

  • 我试图使一个客户端-服务器包成一个应用程序。因此,当应用程序打开时,服务器会自动进入待机/监听模式。问题是当运行服务器套接字时,它直到之后才显示gui。我希望gui和服务器套接字同时发生。 我已经注释掉了代码行,它按照预期工作,直到 我读过关于线程来解决这个问题的文章,但是我看到的例子都没有给出足够清晰的答案。我对python和编程还是相当陌生的。谢啦

  • 我遵循教程https://docs.konghq.com/install/docker/?_ga=2.46631079.1600317329.1601919139-2083746525.1601653016 在Docker中运行API网关 错误 从Web浏览器,转到:http://localhost:8001 我还关闭了Tomcat docker

  • 基本上,我正在Python中的一个订阅计数器应用程序的“草案”上工作。我使用YouTube数据API从YouTube获取数据,然后循环这段代码以更新订阅者计数。但是由于我的GUI代码在循环之后,它永远不会开始,因为循环是无限的,永远不会结束。我尝试将GUI部分放在代码之前,以获得子计数,但没有定义任何变量,因此返回错误。所以基本上,我的问题是如何重新组织它,使其工作,子计数在GUI中更新。我听说过