JavaFX TableView - 无法显示行

时间:2018-03-31 19:44:54

标签: 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 请帮助!!!

1 个答案:

答案 0 :(得分:0)

你的问题非常简单,你还没有在fxml中指定控制器。 只需添加mail 在fxml文件的第一行末尾。 所以整行应该是:

fx:controller="packageName.NewInvoiceController"

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