Javafx PropertyValueFactory 不填充 Tableview [英] Javafx PropertyValueFactory not populating Tableview

查看:24
本文介绍了Javafx PropertyValueFactory 不填充 Tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我困惑了一段时间,我似乎无法掌握它.我正在使用 Cell Value Factory 来填充一个简单的单列表,但它没有填充到表中.

This has baffled me for a while now and I cannot seem to get the grasp of it. I'm using Cell Value Factory to populate a simple one column table and it does not populate in the table.

确实如此,我单击填充的行,但在其中没有看到任何值 - 在本例中为字符串值.[我刚刚编辑了这个以使其更清晰]

It does and I click the rows that are populated but I do not see any values in them- in this case String values. [I just edited this to make it clearer]

我有一个不同的项目,它在相同类型的数据模型下工作.我做错了什么?

I have a different project under which it works under the same kind of data model. What am I doing wrong?

这是代码.不过,最后的注释代码似乎有效.我已经检查过是否存在常见的错误——创建一个新的列实例或一个新的 tableview 实例.没有什么.请帮忙!

Here's the code. The commented code at the end seems to work though. I've checked to see if the usual mistakes- creating a new column instance or a new tableview instance, are there. Nothing. Please help!

//简单数据模型股票.java

//Simple Data Model Stock.java

public class Stock {

    private SimpleStringProperty stockTicker;

    public Stock(String stockTicker) {
        this.stockTicker = new SimpleStringProperty(stockTicker);
    }

    public String getstockTicker() {
        return stockTicker.get();
    }

    public void setstockTicker(String stockticker) {
        stockTicker.set(stockticker);
    }
}

//控制器类MainGuiController.java

//Controller class MainGuiController.java

    private ObservableList<Stock> data;
    @FXML
    private TableView<Stock> stockTableView;// = new TableView<>(data);
    @FXML
    private TableColumn<Stock, String> tickerCol;


    private void setTickersToCol() {
    try {
        Statement stmt = conn.createStatement();//conn is defined and works
        ResultSet rsltset = stmt.executeQuery("SELECT ticker FROM tickerlist order by ticker");
        data = FXCollections.observableArrayList();
        Stock stockInstance;
        while (rsltset.next()) {
            stockInstance = new Stock(rsltset.getString(1).toUpperCase());
            data.add(stockInstance);
        }
    } catch (SQLException ex) {
        Logger.getLogger(WriteToFile.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("Connection Failed! Check output console");
    }

    tickerCol.setCellValueFactory(new PropertyValueFactory<Stock,String>("stockTicker"));
    stockTableView.setItems(data);
    }

    /*THIS, ON THE OTHER HAND, WORKS*/
    /*Callback<CellDataFeatures<Stock, String>, ObservableValue<String>> cellDataFeat =
            new Callback<CellDataFeatures<Stock, String>, ObservableValue<String>>() {
        @Override
        public ObservableValue<String> call(CellDataFeatures<Stock, String> p) {
            return new SimpleStringProperty(p.getValue().getstockTicker());
        }
    };*/

推荐答案

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

代替:

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

写:

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

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

For more information, see this answer:

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

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

如何修复

你的 getter 和 setter 方法的情况是错误的.

The case of your getter and setter methods are wrong.

getstockTicker 应该是 getStockTicker

setstockTicker 应该是 setStockTicker

一些背景资料

您的 PropertyValueFactory 保持不变:

Your PropertyValueFactory remains the same with:

new PropertyValueFactory<Stock,String>("stockTicker")

当您还向 Stock 类添加属性访问器时,命名约定似乎更加明显:

The naming convention will seem more obvious when you also add a property accessor to your Stock class:

public class Stock {

    private SimpleStringProperty stockTicker;

    public Stock(String stockTicker) {
        this.stockTicker = new SimpleStringProperty(stockTicker);
    }

    public String getStockTicker() {
        return stockTicker.get();
    }

    public void setStockTicker(String stockticker) {
        stockTicker.set(stockticker);
    }

    public StringProperty stockTickerProperty() {
        return stockTicker;
    }
}

PropertyValueFactory 使用反射来查找相关访问器(这些应该是公共的).首先,它会尝试使用 stockTickerProperty 访问器,如果不存在,则回退到 getter 和 setter.建议提供属性访问器,因为这样您将自动启用您的表来观察底层模型中的属性,并随着底层模型的变化动态更新其数据.

The PropertyValueFactory uses reflection to find the relevant accessors (these should be public). First, it will try to use the stockTickerProperty accessor and, if that is not present fall back to getters and setters. Providing a property accessor is recommended as then you will automatically enable your table to observe the property in the underlying model, dynamically updating its data as the underlying model changes.

这篇关于Javafx PropertyValueFactory 不填充 Tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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