Javafx TableView未在所有列中显示数据

发布于 2025-01-27 18:02:35 字数 8119 浏览 1 评论 0原文

好的,到了几个星期的新爪哇,但已经编程已有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和最新的&amp;最伟大的内容,或者我想对

任何看答案的人说新的令人兴奋的技术(下一个!),但仍然没有完全解决,以下是使代码正常工作的更改。

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;
        }
    }
}

OK, new to java by several weeks, but have been programming for 30 years. The following code executes, but only the first column is showing anything. The data object is showing multiple rows of data, with fields of data that are filled in. I'm sure I'm missing something, and have looked through similar questions on here.

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;
        }
    }
}

and 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>

THANK YOU for the answer. Way to many years in case insensitive languages. This has been a quick and dirty exercise for me to learn java and the latest & greatest stuff or as I like to say New Exciting Technology (NExT!)

For anyone looking at the answer and still not completely clued in, here are the changes that made the code work properly.

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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下壹個目標 2025-02-03 18:02:35

这个问题确实是一个重复:专门解决您的特定案例,因此很明显。

建议的解决方案(使用lambda,而不是属性valuefactory)

而不是:

aColumn.setCellValueFactory(new PropertyValueFactory<Appointment,LocalDate>("date"));

写入:

aColumn.setCellValueFactory(cellData -> cellData.getValue().dateProperty());

有关更多信息,请参阅此答案:


使用PropertyValueFactory 的解决方案

上面概述的Lambda解决方案是首选,但是如果您希望使用属性valueFactory,则此替代解决方案会提供有关此的信息。

背景

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

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

其中“名称”是属性valueFactory构造函数中指定的字符串。 Getter和Setter中财产名称的第一个字母已大写(通过Java Bean命名大会)。

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

您有这三个表达式:

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

对于您的示例属性,属性valueFactory将寻找这些方法:

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

并且您在sresult class上有这三个getters :

getDateEntered()
getcDesc()
getEnteredBy()

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

建议

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

采用 java camel casel case nam naming caysing cays nam naming cays 也使Java开发人员更容易让Java开发人员更容易阅读您的代码。

This question is really a duplicate of: Javafx PropertyValueFactory not populating Tableview, but I'll specifically address your specific case, so it's clear.

Suggested solution (use a Lambda, not a PropertyValueFactory)

Instead of:

aColumn.setCellValueFactory(new PropertyValueFactory<Appointment,LocalDate>("date"));

Write:

aColumn.setCellValueFactory(cellData -> cellData.getValue().dateProperty());

For more information, see this answer:


Solution using PropertyValueFactory

The lambda solution outlined above is preferred, but if you wish to use PropertyValueFactory, this alternate solution provides information on that.

Background

PropertyValueFactory uses reflection to determine the methods to get and set data values as well as to retrieve bindable properties from your model class. The pattern followed is:

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

Where "name" is the string specified in the PropertyValueFactory constructor. The first letter of the property name in the getter and setter is capitalized (by java bean naming convention).

Why your application doesn't work

You have these three expressions:

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

For your sample properties, the PropertyValueFactory will look for these methods:

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

And you have these three getters on your sresult class:

getDateEntered()
getcDesc()
getEnteredBy()

Only getDateEntered() is going to be picked up by the PropertyValueFactory because that is the only matching method defined in the sresult class.

Advice

You will have to adopt Java standards if you want the reflection in PropertyValueFactory to work (the alternative is to not use the PropertyValueFactory and instead write your own cell factories from scratch).

Adopting Java camel case naming conventions also makes it easier for Java developers to read your code.

能怎样 2025-02-03 18:02:35

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

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

gettergetcdesc cdesc列可能无法显示数据。如果将代码更改为

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

GettergetCdesc cdesc列可能会显示数据。

Some times columns doesn't show data because of column names. eg,

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

and getter is getcDesc cDesc column may not display data. If you change code to

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

and getter is getCDesc CDesc column may display data.

趁微风不噪 2025-02-03 18:02:35

对于任何在经历上述过程后仍​​然没有得到它的人来说,我的问题是我没有用“公共最终”名称指定我的二传手。

For anyone else who still wasn't getting it after going through the above, my problem was that I wasn't specifying my setters with the "public final" designation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文