Javafx tableview没有显示所有列中的数据

时间:2013-09-24 00:33:13

标签: javafx tableview

好的,几个星期以来java新手,但已经编程了30年。执行以下代码,但只有第一列显示任何内容。数据对象显示多行数据,并填入数据字段。我确定我遗漏了一些内容,并在此处查看了类似的问题。

APVoucher_batchgridController.java

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
import javafx.scene.input.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 * FXML Controller class
 *
 * @author kmitchell
 */
public class APVoucher_batchgridController implements Initializable {

    public TableView tblMainList;
    public TableColumn colDateEntered;
    public TableColumn colCreatedBy;
    public TableColumn colDescription;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }

    @FXML
    public void opentables(ActionEvent event) {

        Object forName = null;
        Connection conn = null;
        Statement stmt = null;

        ResultSet rs = null;

        colDateEntered.setCellValueFactory(new PropertyValueFactory<sresult, String>("DateEntered"));

        colDescription.setCellValueFactory(new PropertyValueFactory<sresult, String>("cDesc"));

        colCreatedBy.setCellValueFactory(new PropertyValueFactory<sresult, String>("CreatedBy"));

        try {
            // load the driver into memory
            forName = Class.forName("jstels.jdbc.dbf.DBFDriver2");

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(APVoucher_batchgridController.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            conn = DriverManager.getConnection("jdbc:jstels:dbf:e:\\keystone-data\\keyfund\\seymour\\keyfund.dbc");
        } catch (SQLException ex) {
            Logger.getLogger(APVoucher_batchgridController.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (conn != null) {
            try {
                stmt = conn.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(APVoucher_batchgridController.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (stmt != null) {

                // execute a query
                try {
                    ObservableList<Object> data = FXCollections.observableArrayList();

                    rs = stmt.executeQuery("SELECT denteredon, cdesc, ccreatedby FROM apvbatch WHERE ldeleted = false ORDER BY denteredon DESC");

                    while (rs.next()) {

                        String enteredon = rs.getString("denteredon");
                        String desc = rs.getString("cdesc");
                        String createdby = rs.getString("ccreatedby");

                        sresult row = new sresult(createdby, enteredon, desc);

                        data.add(row);
                    }

                    tblMainList.setItems(data);

                    tblMainList.setVisible(true);


                } catch (SQLException ex) {
                    Logger.getLogger(APVoucher_batchgridController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public class sresult {

        private String DateEntered;
        private String EnteredBy;
        private String cDesc;

        public sresult(String T, String d, String c) {
            this.EnteredBy = T;
            this.DateEntered = d;
            this.cDesc = c;
        }

        public String getEnteredBy() {
            return EnteredBy;
        }

        public void setEnteredBy(String T) {
            EnteredBy = T;
        }

        public String getDateEntered() {
            return DateEntered;
        }

        public void setDateEntered(String d) {
            DateEntered = d;
        }

        public String getcDesc() {
            return cDesc;
        }

        public void setcDesc(String c) {
            cDesc = c;
        }
    }
}

和APVoucher_batchgrid.fxml

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

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" fx:id="batchlistform" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="keystone.APVoucher_batchgridController">
  <children>
    <BorderPane layoutX="0.0" layoutY="0.0" prefHeight="400.0" prefWidth="600.0">
      <center>
        <AnchorPane prefHeight="-1.0" prefWidth="-1.0">
          <children>
            <Pane layoutX="0.0" layoutY="0.0" prefHeight="53.0" prefWidth="580.0">
              <children>
                <Label layoutX="7.0" layoutY="9.0" prefWidth="202.0" text="AP Vouchers Batch List">
                  <font>
                    <Font name="System Bold" size="14.0" />
                  </font>
                </Label>
                <Button fx:id="btnClose" cancelButton="true" layoutX="513.0" layoutY="27.0" mnemonicParsing="false" text="Close" />
                <Button id="btnClose" fx:id="apvRefresh" cancelButton="true" layoutX="185.0" layoutY="27.0" mnemonicParsing="false" onAction="#opentables" text="Refresh" />
              </children>
            </Pane>
            <TableView fx:id="tblMainList" layoutX="0.0" layoutY="53.0" prefHeight="323.0" prefWidth="580.0">
              <columns>
                <TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="91.0" text="Date Entered" fx:id="colDateEntered" />
                <TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="100.0" text="Created By" fx:id="colCreatedBy" />
                <TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="261.0" text="Description" fx:id="colDescription" />
              </columns>
            </TableView>
          </children>
        </AnchorPane>
      </center>
      <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
      </padding>
    </BorderPane>
  </children>
  <stylesheets>
    <URL value="@apvoucher_batchgrid.css" />
  </stylesheets>
</AnchorPane>

谢谢你的回答。在不区分大小写的语言中使用多年的方法。对我来说,学习java和最新版本是一个快速而又肮脏的练习。最棒的东西或者我喜欢说新的激动人心的技术(NExT!)

对于那些正在寻找答案而仍未完全了解的人来说,以下是使代码正常运行的更改。

colDateEntered.setCellValueFactory(new PropertyValueFactory<sresult, String>("Denteredon"));
colDescription.setCellValueFactory(new PropertyValueFactory<sresult, String>("CDesc"));
colEnteredBy.setCellValueFactory(new PropertyValueFactory<sresult, String>("Ccreatedby"));


    public class sresult {

        private String Denteredon;
        private String Ccreatedby;
        private String CDesc;

        public sresult(String T, String d, String c) {
            this.Ccreatedby = T;
            this.Denteredon = d;
            this.CDesc = c;
        }

        public String getCcreatedby() {
            return Ccreatedby;
        }

        public void setCreatedby(String T) {
            Ccreatedby = T;
        }

        public String getDenteredon() {
            return Denteredon;
        }

        public void setDenteredon(String d) {
            Denteredon = d;
        }

        public String getCDesc() {
            return CDesc;
        }

        public void setCDesc(String c) {
            CDesc = c;
        }
    }
}

3 个答案:

答案 0 :(得分:38)

这个问题实际上与Javafx PropertyValueFactory not populating Tableview重复,但我会专门针对您的具体案例,所以很清楚。

背景

PropertyValueFactory使用反射来确定获取和设置数据值的方法,以及从模型类中检索可绑定属性。接下来的模式是:

PropertyValueType getName()
void setName(PropertyValueType value)
PropertyType nameProperty()

其中“name”是PropertyValueFactory构造函数中指定的字符串。 getter和setter中属性名的第一个字母是大写的(通过java bean命名约定)。

为什么您的应用程序不起作用

你有这三个表达式:

new PropertyValueFactory<sresult, String>("DateEntered")
new PropertyValueFactory<sresult, String>("cDesc")
new PropertyValueFactory<sresult, String>("CreatedBy")

对于您的样本属性,PropertyValueFactory将查找以下方法:

"DateEntered" => getDateEntered()
"cDesc" => getCDesc()
"CreatedBy" => getCreatedBy()

你的sresult课上有三个吸气剂:

getDateEntered()
getcDesc()
getEnteredBy()

PropertyValueFactory将仅拾取getDateEntered(),因为这是sresult类中定义的唯一匹配方法。

建议

如果您希望PropertyValueFactory中的反射起作用,则必须采用Java标准(替代方法是不使用PropertyValueFactory,而是从头开始编写自己的单元工厂)。

采用Java camel case naming conventions也使Java开发人员更容易阅读您的代码。

答案 1 :(得分:0)

有时由于列名,列不显示数据。例如,

new PropertyValueFactory<sresult, String>("cDesc")

gettergetcDesc cDesc 列可能不显示数据。如果您将代码更改为

new PropertyValueFactory<sresult, String>("CDesc")

gettergetCDesc CDesc 列可以显示数据。

答案 2 :(得分:-1)

对于那些在完成上述操作后仍然没有得到它的人,我的问题是我没有用“公共最终”指定我的安装人员。