我试图使用javafx制作GUI程序。该程序应该从csv文件中获取数据,将其存储在公共列表中,并通过控制器类将其显示在GUI的javafx TableView中。
这是控制器代码
package controllers;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import application.Main;
import application.TablaEst;
public class ControllerVer1 implements Initializable {
public Stage stage = new Stage();
private ObservableList<TablaEst> data = FXCollections.observableArrayList();
@FXML
private Button volverButton;
@FXML
private TableView<TablaEst> tableData;
//se crean columnas
@FXML
private TableColumn<TablaEst, String> nameCol;
@FXML
private TableColumn<TablaEst, String> matCol;
@FXML
private TableColumn<TablaEst, String> carCol;
@FXML
private TableColumn<TablaEst, String> ingCol;
@FXML
private TableColumn<TablaEst, String> promCol;
@FXML
private TableColumn<TablaEst, String> estCol;
@Override
public void initialize(URL location, ResourceBundle resources) {
nameCol.setCellValueFactory(new PropertyValueFactory<TablaEst,String>("Nombre"));
matCol.setCellValueFactory(new PropertyValueFactory<TablaEst,String>("Matrícula"));
carCol.setCellValueFactory(new PropertyValueFactory<TablaEst,String>("Carrera"));
ingCol.setCellValueFactory(new PropertyValueFactory<TablaEst,String>("Ingreso"));
promCol.setCellValueFactory(new PropertyValueFactory<TablaEst,String>("Promedio"));
estCol.setCellValueFactory(new PropertyValueFactory<TablaEst,String>("Estatus"));
tableData.setEditable(true);
for (int i=0; i < (Main.estudiantes).size(); i++){
data.add(new TablaEst(
(Main.estudiantes).get(i)[0],
(Main.estudiantes).get(i)[1],
(Main.estudiantes).get(i)[2],
(Main.estudiantes).get(i)[3],
(Main.estudiantes).get(i)[4],
(Main.estudiantes).get(i)[5]));
//for (int j=0; j<(Main.estudiantes.get(i)).length; j++){
//}
System.out.println(data.get(i));
}
tableData.setItems(data);
}
@FXML
public void volverAMain(ActionEvent e) throws IOException{
stage = (Stage)volverButton.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource("/application/MainGUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
这是我的构造函数类
package application;
import javafx.beans.property.SimpleStringProperty;
public class TablaEst {
private final SimpleStringProperty name;
private final SimpleStringProperty mat;
private final SimpleStringProperty car;
private final SimpleStringProperty ing;
private final SimpleStringProperty prom;
private final SimpleStringProperty est;
public TablaEst (String sName, String sMat, String sCar, String sIng, String sProm, String sEst) {
this.name = new SimpleStringProperty(sName);
this.mat = new SimpleStringProperty(sMat);
this.car = new SimpleStringProperty(sCar);
this.ing = new SimpleStringProperty(sIng);
this.prom = new SimpleStringProperty(sProm);
this.est = new SimpleStringProperty(sEst);
}
//Getter methods
public String getName(){
return name.get();
}
public String getMat(){
return mat.get();
}
public String getCar(){
return car.get();
}
public String getIng(){
return ing.get();
}
public String getProm(){
return prom.get();
}
public String getEst(){
return est.get();
}
//setter methods
public void setName(String nName){
name.set(nName);
}
public void setMat(String nMat){
mat.set(nMat);
}
public void setCar(String nCar){
car.set(nCar);
}
public void setIng(String nIng){
ing.set(nIng);
}
public void setProm(String nProm){
prom.set(nProm);
}
public void setEst(String nEst){
est.set(nEst);
}
}
我已经验证了我在主类中的学习者列表正在从csv中读取信息,但是该表在GUI上仍然显示为空白。
任何反馈和帮助表示赞赏。
最佳答案
线
nameCol.setCellValueFactory(new PropertyValueFactory<TablaEst,String>("Nombre"));
表示
nameCol
表列应绑定TablaEst.NombreProperty()
,如果不存在,则将TablaEst.getNombre()
实例的TablaEst
字段呈现为单元格值。由于没有这样的方法,因此该列将为空。您正在TablaEst类中使用
SimpleStringProperty
。除了普通的获取器/设置器,您还应该添加特定于JavaFX的获取器:private final SimpleStringProperty name;
...
public String getName(){
return name.get();
}
public void setName(String nName){
name.set(nName);
}
public StringProperty nameProperty(){
return name;
}
适用于所有领域。如果tableview将是可编辑的,则此新的getter将提供对基础TablaEst实例的即时更新。
现在您可以定义
nameCol.setCellValueFactory(new PropertyValueFactory<TablaEst,String>("name"));
表列的标题名称应通过设置
text
属性来指定:nameCol.setText("The Nombre");
或在FXML中
<TableColumn text="The Nombre" .... >