JavaFX TableView - 无法显示行

标签 java tableview javafx-8

为自己开发一个简单的发票软件。就卡在第一点了。 无法在 TableView 中显示行。我已经尝试了所有可能的解决方案,浏览了许多教程视频、指南等。仍然无法找到问题 请帮忙

NewInvoice.fxml

    <TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
  <tabs>
     <Tab text="CASH">
     <content>
     <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
           <children>
              <Button fx:id="additembutton" layoutX="412.0" layoutY="41.0" mnemonicParsing="false" text="ADD" />
              <TableView fx:id="invoiceitemtableview" layoutX="190.0" layoutY="180.0" prefHeight="200.0" prefWidth="481.0">
                <columns>
                    <TableColumn fx:id="amountcol" prefWidth="75.0" text="Amount" />
                    <TableColumn fx:id="gstcol" prefWidth="75.0" text="GST" />
                    <TableColumn fx:id="subtotalcol" prefWidth="75.0" text="Subtotal" />
                    <TableColumn fx:id="cgstcol" prefWidth="75.0" text="CGST" />
                    <TableColumn fx:id="sgstcol" prefWidth="75.0" text="SGST" />
                </columns>
              </TableView>
              <TextField fx:id="itemcodetextbox" layoutX="216.0" layoutY="41.0" />
           </children></AnchorPane>
  </content>
</Tab>
<Tab text="BOBCARD">
  <content>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
  </content>
</Tab>

NewInvoiceController.java

public class NewInvoiceController implements Initializable {

@FXML
TableView<InvoiceItemBean> invoiceitemtableview;
@FXML
TableColumn<InvoiceItemBean,String> amountcol, gstcol, subtotalcol, cgstcol, sgstcol;


final ObservableList<InvoiceItemBean> data = FXCollections.observableArrayList(
    new InvoiceItemBean("1049","5","999.10","22.5","22.5"),
    new InvoiceItemBean("1800","12","1180.10","305.00","305.00")
    );


@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO


    amountcol.setCellValueFactory(new PropertyValueFactory<>("amount"));
    gstcol.setCellValueFactory(new PropertyValueFactory<>("gst"));
    subtotalcol.setCellValueFactory(new PropertyValueFactory<>("subtotal"));
    cgstcol.setCellValueFactory(new PropertyValueFactory<>("cgst"));
    sgstcol.setCellValueFactory(new PropertyValueFactory<>("sgst"));
    invoiceitemtableview.setItems(data);}    
}

InvoiceItemBean.java

public class InvoiceItemBean {
    private final SimpleStringProperty amount;
    private final SimpleStringProperty gst;
    private final SimpleStringProperty subtotal;
    private final SimpleStringProperty cgst;
    private final SimpleStringProperty sgst;
    public InvoiceItemBean( String amount, String gst, String subtotal, String cgst, String sgst) {
        this.amount = new SimpleStringProperty(amount);
        this.gst = new SimpleStringProperty(gst);
        this.subtotal = new SimpleStringProperty(subtotal);
        this.cgst = new SimpleStringProperty(cgst);
        this.sgst = new SimpleStringProperty(sgst);
    }

    public String getAmount() {
        return amount.get();
    }

    public String getGst() {
        return gst.get();
    }

    public String getSubtotal() {
        return subtotal.get();
    }

    public String getCgst() {
        return cgst.get();
    }

    public String getSgst() {
        return sgst.get();
    }

    public void setAmount(String amount_value) {
        amount.set(amount_value);
    }

    public void setGst(String gst_value) {
        gst.set(gst_value);
    }

    public void setSubtotal(String subtotal_value) {
        subtotal.set(subtotal_value);
    }

    public void setCgst(String cgst_value) {
        cgst.set(cgst_value);
    }

    public void setSgst(String sgst_value) {
        sgst.set(sgst_value);
    }
}

Main.java

public class ClimaxInvoice extends Application {

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("NewInvoice.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

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

}

无法获取任何内容

Output Table view showing no content 请帮忙!!!

最佳答案

你的问题很简单,你没有在fxml中指定 Controller 。 只需添加 fx:controller="packageName.NewInvoiceController" 在 fxml 文件第一行的末尾。 所以整行应该是:

<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="packageName.NewInvoiceController">

当然,将 packageName 替换为您的包的名称。

关于JavaFX TableView - 无法显示行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49591726/

相关文章:

java - 获取 javaFX 8 中节点的屏幕坐标

JavaFX ControlsFX 自动完成 : How to get popup result into a new ObservableList

java - 使用任何文件附加程序的 Log4j 每日轮换和每月保留

java - ReSTLet,带参数的 get 示例

快速表格 View 单元格更改宽度框架不起作用

ios - 为什么 TableView 是空的

java - Netty 4.0 : Spawning off, 与许多客户端/对等点维护和通信(TCP)

java - Hangman Java - 替换下划线

ios - 加载TableData之前如何从CloudKit获取数据

java - 执行者或线程防止程序关闭