我试图使用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);
}
}
我已经验证了我的主要类中的estudiantes列表是从csv读取信息,但是该表在GUI上仍然显示为空白。 任何反馈和帮助表示赞赏。
答案 0 :(得分:2)
该行
nameCol.setCellValueFactory(new PropertyValueFactory<TablaEst,String>("Nombre"));
表示nameCol
表列应该绑定TablaEst.NombreProperty()
,或者如果它不存在,则将TablaEst.getNombre()
实例的TablaEst
字段呈现为单元格值。由于没有这样的方法,该列将为空。
您正在使用TablaEst类中的SimpleStringProperty
。除了普通的getter / setter之外,你还应该添加JavaFX特定的getter,如:
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" .... >