【问题标题】:JavaFX - TableView - setItems : The method setItems(ObservableList) in the type TableView is not showing me anythingJavaFX - TableView - setItems:TableView 类型中的方法 setItems(ObservableList) 没有向我显示任何内容
【发布时间】:2020-02-20 02:05:30
【问题描述】:

我是新来的,所以我不知道该怎么做,但我的 tableview.setitems(oblist); 有这个问题 当我启动我的程序时,表格视图不会向我显示该区域内的任何奇怪线条

public class FXMLTableController implements Initializable {
private  Connection con;

@FXML
private TableView<LOC> tableview;
@FXML
private TableColumn<LOC, Integer> ttidLoc;
@FXML
private TableColumn<LOC,Integer> ttidProd;
@FXML
private TableColumn<LOC, Float> ttPrixJour;
@FXML
private TableColumn<LOC,Integer> ttMaxJour;
@FXML
private TableColumn<LOC,Integer> ttidClient;

ObservableList<LOC> oblist=FXCollections.observableArrayList();

@Override
public void initialize(URL location, ResourceBundle resources) {


    try {
         con = DataBase.getInstance().getConnection();
        ResultSet rs = con.createStatement().executeQuery("select * from locations");

        while(rs.next()) {
            oblist.add(new LOC(rs.getInt("idLoc"),rs.getInt("idProd"),rs.getFloat("PrixJour"),rs.getInt("MaxJour"),rs.getInt("idClient")));
        } 

        ttidLoc.setCellValueFactory(new PropertyValueFactory<>("idLoc"));
        ttidProd.setCellValueFactory(new PropertyValueFactory<>("idProd"));
        ttPrixJour.setCellValueFactory(new PropertyValueFactory<>("PrixJour"));
        ttMaxJour.setCellValueFactory(new PropertyValueFactory<>("MaxJour"));
        ttidClient.setCellValueFactory(new PropertyValueFactory<>("idClient"));



       tableview.setItems(oblist);
    } catch (SQLException ex) {
        Logger.getLogger(FXMLTableController.class.getName()).log(Level.SEVERE, null, ex);
    }


}

}

这是我使用的实体代码

public class LOC {
private int idLoc;
private int idProd;
private float PrixJour;
private int MaxJour;
private int idClient;

public LOC(int idLoc, int idProd, float PrixJour, int MaxJour, int idClient) {
    this.idLoc = idLoc;
    this.idProd = idProd;
    this.PrixJour = PrixJour;
    this.MaxJour = MaxJour;
    this.idClient=idClient;
}

public LOC( int idProd, float PrixJour, int MaxJour, int idClient) {

    this.idProd = idProd;
    this.PrixJour = PrixJour;
    this.MaxJour = MaxJour;
    this.idClient=idClient;

顺便说一句,我正在使用场景构建器,我试图在单击第一个按钮时在另一个场景中打开 tableview

【问题讨论】:

标签: javafx tableview


【解决方案1】:

你也可以使用你自己的 CellValueFactory 实现,这样你就不需要依赖 PropertyValueFactory 的反射逻辑。举个例子:

ttidLoc.setCellValueFactory(param -> {
    LOC value = param.getValue();
    return new SimpleObjectProperty<>(value.idLoc);
});

【讨论】:

  • 想发表评论,但我无法在那里格式化代码:(
  • 是的,cmets 中的代码看起来很糟糕,因为没有换行符......但是您可以通过反引号将单行代码格式化为代码。
  • 我确实发生了同样的问题,我注意到如果我删除所有单元格值并单独留下 setitems,同样的东西会显示出来,所以它不会填满我的单元格
  • 好吧,也许 ObservableList 中没有添加任何内容。那你应该去调试一下。在while循环后设置断点检查列表中是否有任何东西,或者只是打印出列表或列表大小。
  • 如果列表中有内容,请尝试将其分解为具有一个字段和一个硬编码实例等的对象。并创建一个最小的不工作示例,以便有人可以运行此示例并对其进行调试.
猜你喜欢
  • 2018-01-09
  • 1970-01-01
  • 2019-03-05
  • 2016-09-02
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 2021-10-02
  • 2012-12-30
相关资源
最近更新 更多