我的表JavaFX / SceneBuilder中没有显示数据

时间:2017-04-21 21:03:31

标签: 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);
    }   

}

TableViewController

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>

1 个答案:

答案 0 :(得分:0)

您尚未在FXML文件中指定控制器类。您需要在根元素中添加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像素的高度,这将使得查看任何内容变得有些困难。从表格视图中删除fixedCellSize属性。