java - 数据未显示在我的表 JavaFX/SceneBuilder 中

标签 java xml eclipse javafx scenebuilder

我已经遵循了许多关于如何在表格中填充单元格的教程,但当我运行程序时,我似乎无法让它们显示出来。知道我做错了什么以及如何解决它吗?

谢谢,

package application;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;


public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("GUISCENEBUILDER.fxml"));
            Scene scene = new Scene(root,850,700);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

属性

package application;

import javafx.beans.property.SimpleStringProperty;

public class Product {
    private final SimpleStringProperty windowTime;
    private final SimpleStringProperty emailTime;


    public Product(String windowTime, String emailTime) {
        this.windowTime = new SimpleStringProperty(windowTime);
        this.emailTime = new SimpleStringProperty(emailTime);
    }

    public String getWindowTime() {
        return windowTime.get();
    }

    public void setWindowTime(String newTime) {
        windowTime.set(newTime);
    }

    public String getEmailTime() {
        return emailTime.get();
    }

    public void setEmailTime(String newTime) {
        emailTime.set(newTime);
    }   

}

TableView Controller

package application;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class TableViewController implements Initializable {
    @FXML
    private TableView<Product> tableID;

    @FXML
    private TableColumn<Product, String> windowTime;

    @FXML
    private TableColumn<Product, String> emailTime;

    /*
     * Get all the products
     */
    ObservableList<Product> data = FXCollections.observableArrayList(
        new Product("7:00am - 8:30am", "8:30am"),
        new Product("10:00am - 11:10am", "11:10am"),
        new Product("12:00pm - 1:30pm", "1:30pm"),
        new Product("3:00pm - 4:30pm", "4:30pm")
    );

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        windowTime.setCellValueFactory(new PropertyValueFactory<Product, String>("windowTime"));
        emailTime.setCellValueFactory(new PropertyValueFactory<Product, String>("emailTime"));

        tableID.setItems(data);
    }

}

FXML

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

<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>

<BorderPane  maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="850.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <TabPane prefHeight="472.0" prefWidth="850.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
        <tabs>
          <Tab text="Press 1">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="526.0" prefWidth="805.0">
                     <children>
                        <ImageView fitHeight="559.0" fitWidth="850.0" layoutY="-7.0">
                           <image>
                              <Image url="@1.jpg" />
                           </image>
                        </ImageView>
                        <TableView fx:id="tableID" fixedCellSize="1.0" layoutX="594.0" layoutY="55.0">
                           <columns>
                              <TableColumn fx:id="windowTime" prefWidth="75.0" text="Window Times" />

                              <TableColumn fx:id="emailTime" prefWidth="75.0" text="Email Times" />
                           </columns>
                           <columnResizePolicy>
                              <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                           </columnResizePolicy>
                        </TableView>
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
          <Tab text="Press 2">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <ImageView fitHeight="561.0" fitWidth="850.0" layoutY="-7.0">
                           <image>
                              <Image url="@1.jpg" />
                           </image>
                        </ImageView>
                        <TableView fx:id="tableID" fixedCellSize="1.0" layoutX="594.0" layoutY="55.0">
                           <columns>
                              <TableColumn fx:id="windowTime" prefWidth="75.0" text="Window Times" />
                              <TableColumn fx:id="emailTime" prefWidth="75.0" text="Email Times" />
                           </columns>
                           <columnResizePolicy>
                              <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                           </columnResizePolicy>
                        </TableView>
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
            <Tab fx:id="tableID" text="Press 3">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <ImageView fitHeight="559.0" fitWidth="850.0" layoutY="-7.0">
                           <image>
                              <Image url="@1.jpg" />
                           </image>
                        </ImageView>
                        <TableView fx:id="tableID" fixedCellSize="1.0" layoutX="594.0" layoutY="55.0">
                          <columns>
                            <TableColumn fx:id="windowTime" prefWidth="75.0" text="Window Times" />
                            <TableColumn fx:id="emailTime" prefWidth="75.0" text="Email Times" />
                          </columns>
                           <columnResizePolicy>
                              <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                           </columnResizePolicy>
                        </TableView>
                     </children>
                  </AnchorPane>
              </content>
            </Tab>
        </tabs>
      </TabPane>
   </center>
   <top>
      <Pane prefHeight="126.0" prefWidth="850.0" BorderPane.alignment="CENTER">
         <children>
            <ImageView fitHeight="126.0" fitWidth="850.0">
               <image>
                  <Image url="@1.1.jpg" />
               </image>
            </ImageView>
            <Button layoutX="14.0" layoutY="23.0" mnemonicParsing="false" style="-fx-background-color: #9EBCD2;" text="Start">
               <font>
                  <Font size="38.0" />
               </font>
               <effect>
                  <InnerShadow color="#b5b5b5" height="32.77" radius="15.907499999999999" width="32.86" />
               </effect>
               <cursor>
                  <Cursor fx:constant="HAND" />
               </cursor>
            </Button>
            <Button depthTest="ENABLE" layoutX="154.0" layoutY="23.0" mnemonicParsing="false" style="-fx-background-color: #9EBCD2;" text="Stop">
               <font>
                  <Font size="38.0" />
               </font>
               <effect>
                  <InnerShadow blurType="ONE_PASS_BOX" color="#b5b5b5" height="32.67" radius="15.8825" width="32.86" />
               </effect>
               <cursor>
                  <Cursor fx:constant="HAND" />
               </cursor>
            </Button>
         </children>
         <cursor>
            <Cursor fx:constant="DEFAULT" />
         </cursor>
      </Pane>
   </top>
</BorderPane>

最佳答案

您尚未在 FXML 文件中指定 Controller 类。您需要向根元素添加 fx:controller 属性:

<BorderPane  fx:controller="application.TableViewController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="850.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">

此外,您的所有表格都具有相同的 fx:id,相应的表格列也是如此,这是没有意义的,并且您的一个选项卡也(莫名其妙地)与表格共享相同的 fx:id。 Id 应该是唯一的。

最后,您将表格行的高度设置为 1 像素,这将使查看任何内容变得有些困难。从表格 View 中删除 fixedCellSize 属性。

关于java - 数据未显示在我的表 JavaFX/SceneBuilder 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43551981/

相关文章:

java - 如何在AsyncTask doInBackground()中使用同步对象?

c - 为什么我的程序结合了两个 printf 命令?

java - 使用父类(super class)表中类的方法 (Java)

Java Swing : drawLine very slow

xml - GPX Schema 验证问题

java.lang.RuntimeException : Unable to start activity ComponentInfo in Android

c++ - 为什么在两个不同的类中调用 TinyXPath 时,同一对象会给出不同的结果?

java - 在 Tomcat 中启用 Context reload ="true"时,JDBC 连接池连接不足

java - mvn测试并编译

用于管理 Apple 推送通知的 Java 库