【问题标题】:TableView not showing any value Javafx 8 [duplicate]TableView没有显示任何价值Javafx 8 [重复]
【发布时间】:2021-10-02 01:22:57
【问题描述】:

我正在处理一个项目,我需要使用数据库中的数据填充 TableView。在同一个项目中,我与其他 TableView 合作过,到目前为止我没有任何问题。但是我已经尝试了很多东西,检查了解决方案:Javafx tableview not showing data in all columns 也没有工作,我已经不知道该怎么做了,希望你能帮助我,我给你看代码:

TableView 列填充:

private void fillColumns(){
    TableColumn<Exam, String> column1 = new TableColumn<>("Estudio");
    column1.setCellValueFactory(new PropertyValueFactory<>("estudio"));
    
    TableColumn<Exam, String> column2 = new TableColumn<>("Doctor");
    column2.setCellValueFactory(new PropertyValueFactory<>("doctor"));
    
    TableColumn<Exam, String> column3 = new TableColumn<>("Fecha");
    column3.setCellValueFactory(new PropertyValueFactory<>("fecha"));
    
    TableColumn<Exam, String> column4 = new TableColumn<>("Descripcion");
    column4.setCellValueFactory(new PropertyValueFactory<>("descripcion"));
    
    TableColumn<Exam, String> column5 = new TableColumn<>("Precio");
    column5.setCellValueFactory(new PropertyValueFactory<>("precio"));
    
    TableColumn<Exam, String> column6 = new TableColumn<>("Paciente");
    column6.setCellValueFactory(new PropertyValueFactory<>("paciente"));
    
    //add columns
    tvExam.getColumns().clear();
    tvExam.getColumns().add(column1);
    tvExam.getColumns().add(column2);
    tvExam.getColumns().add(column3);
    tvExam.getColumns().add(column4);
    tvExam.getColumns().add(column5);
    tvExam.getColumns().add(column6);
    
    column1.prefWidthProperty().bind(tvExam.widthProperty().multiply(0.2));
    column2.prefWidthProperty().bind(tvExam.widthProperty().multiply(0.3));
    column3.prefWidthProperty().bind(tvExam.widthProperty().multiply(0.1));
    column4.prefWidthProperty().bind(tvExam.widthProperty().multiply(0.2));
    column5.prefWidthProperty().bind(tvExam.widthProperty().multiply(0.1));
    column6.prefWidthProperty().bind(tvExam.widthProperty().multiply(0.1));
}

我的考试班:

public class Exam {
    protected SimpleStringProperty estudio = null; 
    protected SimpleStringProperty doctor = null; 
    protected SimpleStringProperty fecha =  null; 
    protected SimpleStringProperty descripcion = null;
    protected SimpleFloatProperty precio;
    protected SimpleStringProperty paciente = null;
    
    public Exam() {
        
    }

    public Exam(String estudio, String doctor, String fecha, String descripcion, float precio, String paciente){
        this.estudio = new SimpleStringProperty(estudio);
        this.descripcion = new SimpleStringProperty(descripcion);
        this.doctor = new SimpleStringProperty(doctor); 
        this.fecha = new SimpleStringProperty(fecha); 
        this.precio = new SimpleFloatProperty(precio);
        this.paciente = new SimpleStringProperty(paciente);
    }

    public String getpaciente() {
        return paciente.get();
    }

    public void setpaciente(String paciente) {
        this.paciente = new SimpleStringProperty(paciente);
    }

    public String getestudio() {
        return estudio.get();
    }

    public void setestudio(String estudio) {
        this.estudio = new SimpleStringProperty(estudio);
    }

    public String getdoctor() {
        return doctor.get();
    }

    public void setdoctor(String doctor) {
        this.doctor = new SimpleStringProperty(doctor);
    }

    public String getfecha() {
        return fecha.get();
    }

    public void setfecha(String fecha) {
        this.fecha = new SimpleStringProperty(fecha);
    }

    public String getdescripcion() {
        return descripcion.get();
    }

    public void setdescripcion(String descripcion) {
        this.descripcion = new SimpleStringProperty(descripcion);
    }

    public float getprecio() {
        return precio.get();
    }

    public void setprecio(float precio) {
        this.precio = new SimpleFloatProperty(precio);
    }
} 

我用数据库中的数据填充函数(我已经检查过并且数据在那里,问题是表格视图):

while (resultSet.next()) {
    Exam nuevo = new Exam();
    
    nuevo.setdoctor(resultSet.getString("fecha"));
    nuevo.setestudio(resultSet.getString("estudio"));
    nuevo.setpaciente(resultSet.getString("paciente"));
    nuevo.setdescripcion(resultSet.getString("descripcion"));
    nuevo.setfecha(resultSet.getString("fecha"));
    nuevo.setprecio(resultSet.getFloat("precio"));
    
    tvExam.getItems().add(fila);
}

【问题讨论】:

标签: java javafx tableview


【解决方案1】:

使用 camelCase 重命名 getter 和 setter。

这是一个java标准:

public String getFoo()
{
     return this.foo;
}

public void setFoo(String foo)
{
     this.foo = foo;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2015-02-02
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 2015-02-01
    相关资源
    最近更新 更多