Javafx tableview 未显示所有列中的数据 [英] Javafx tableview not showing data in all columns

查看:40
本文介绍了Javafx tableview 未显示所有列中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,几周前刚接触 Java,但已经编程 30 年了.以下代码执行,但只有第一列显示任何内容.数据对象显示多行数据,其中填充了数据字段.我确定我遗漏了一些东西,并且已经在此处查看了类似的问题.

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

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

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>

谢谢你的回答.多年不区分大小写的语言.这对我来说是一个快速而肮脏的练习,用于学习 Java 和最新的 &最棒的东西,或者我喜欢说的令人兴奋的新技术(NExT!)

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

推荐答案

这个问题实际上是重复的:Javafx PropertyValueFactory 不填充 Tableview,但我会专门针对您的具体情况,所以很清楚.

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

建议的解决方案(使用 Lambda,而不是 PropertyValueFactory)

代替:

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

写:

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

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

For more information, see this answer:

使用 PropertyValueFactory 的解决方案

首选上述 lambda 解决方案,但如果您希望使用 PropertyValueFactory,则此替代解决方案提供了相关信息.

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

背景

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

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()

名称"在哪里是在 PropertyValueFactory 构造函数中指定的字符串.getter 和 setter 中属性名称的第一个字母大写(按照 java bean 命名约定).

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).

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

你有这三个表达式:

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

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

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

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

并且您的 sresult 类中有这三个 getter:

And you have these three getters on your sresult class:

getDateEntered()
getcDesc()
getEnteredBy()

只有 getDateEntered() 将被 PropertyValueFactory 选取,因为这是 sresult 类中定义的唯一匹配方法.

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

建议

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

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).

采用 Java 驼峰命名约定也让Java 开发人员阅读您的代码.

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

这篇关于Javafx tableview 未显示所有列中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆