Java TableView列数据集设置为小数点后2位

Java TableView列数据集设置为小数点后2位,java,javafx,double,tableview,Java,Javafx,Double,Tableview,我有一个类文件Nepokretnost.java,其中的构造函数如下所示: public Nepokretnost(String tipNepokretnosti, String zona, String pravo, double povrsina, int amortizacija, double osnovica, double kredit, double porez) { this.tipNepokretnosti = tipNepokretnosti;

我有一个类文件Nepokretnost.java,其中的构造函数如下所示:

public Nepokretnost(String tipNepokretnosti, String zona, String pravo, 
        double povrsina, int amortizacija, double osnovica, double kredit, double porez) {
    this.tipNepokretnosti = tipNepokretnosti;
    this.zona = zona;
    this.pravo = pravo;
    this.povrsina = povrsina;
    this.amortizacija = amortizacija;
    this.osnovica = osnovica;
    this.kredit = kredit;
    this.porez = porez; 
}
此外,我还为每个类字段提供了带有列的TableView。我的问题是双字段“povrsina”。我想从TextField设置它

我将名为txtPovrsina的TextField的内容发送到双变量:

double dPovrsina;
dPovrsina = Double.parseDouble(txtPovrsina.getText());
然后将所有字段放入TableView:

ObservableList<Nepokretnost> data = tblTabela.getItems();
    data.add(new Nepokretnost(cboNepokretnost.getValue().toString(),cboZona.getValue().toString(),
            txtPravo,dPovrsina,40,450000.25,2500.00,2500.00));
observeListData=tblTabela.getItems();
data.add(新的Nepokretnost(cboNepokretnost.getValue().toString(),cboZona.getValue().toString(),
txtPravo,dPovrsina,40450000.252500.002500.00);
一切都很好,但我想要一些我不知道如何设置的应用程序行为。现在,当我把int像25放在TextField中时,我在TableView列中得到了25.0。我希望所有列单元格正好有2位小数

我试过:

DecimalFormat df=new DecimalFormat("#.00");
ObservableList<Nepokretnost> data = tblTabela.getItems();
    data.add(new Nepokretnost(cboNepokretnost.getValue().toString(),cboZona.getValue().toString(),
            txtPravo,df.format(dPovrsina),40,450000.25,2500.00,2500.00));
DecimalFormat df=新的DecimalFormat(#.00”);
ObservableList data=tblTabela.getItems();
data.add(新的Nepokretnost(cboNepokretnost.getValue().toString(),cboZona.getValue().toString(),
txtPravo,df.格式(dPovrsina),40450000.252500.002500.00);
但我得到错误“不兼容类型:字符串不能转换为双精度”

我仍然是java的新手,但我猜格式正在生成字符串,我希望输入保持双精度,只保留小数点后2位。像本专栏一样,我对其他双字段也有同样的问题


谁能给我一些指导吗?

您想更改数据在表中的显示方式,而不是更改数据本身。为此,需要在表列上设置单元格工厂。差不多

TableView<Nepokretnost> table = new TableView<>();
TableColumn<Nepokretnost, Number> povrsinaCol = new TableColumn<>("Povrsina");
povrsinaCol.setCellValueFactory(cellData -> 
    new ReadOnlyDoubleWrapper(cellData.getValue().getPovrsina()));

povrsinaCol.setCellFactory(tc -> new TableCell<Nepokretnost, Number>() {
    @Override
    protected void updateItem(Number value, boolean empty) {
        super.updateItem(value, empty) ;
        if (empty) {
            setText(null);
        } else {
            setText(String.format("%0.2f", value.doubleValue()));
        }
    }
});
TableView table=newtableview();
TableColumn povrsinaCol=新的TableColumn(“Povrsina”);
povrsinaCol.setCellValueFactory(cellData->
新的ReadOnlyDoubleWrapper(cellData.getValue().getPovrsina());
povrsinaCol.setCellFactory(tc->new TableCell(){
@凌驾
受保护的void updateItem(数值,布尔空){
super.updateItem(值,空);
if(空){
setText(空);
}否则{
setText(String.format(“%0.2f”,value.doubleValue());
}
}
});

如果在只读包装器上发现值转换错误,请将该值用作对象

TableView<Nepokretnost> table = new TableView<>();
TableColumn<Nepokretnost, Number> povrsinaCol = new TableColumn<>("Povrsina");
povrsinaCol.setCellValueFactory(cellData -> 
    new ReadOnlyDoubleWrapper(cellData.getValue().getPovrsina().asObject()));

povrsinaCol.setCellFactory(tc -> new TableCell<Nepokretnost, Number>() {
    @Override
    protected void updateItem(Number value, boolean empty) {
        super.updateItem(value, empty) ;
        if (empty) {
            setText(null);
        } else {
            setText(String.format("%0.2f", value.doubleValue()));
        }
    }
});
TableView table=newtableview();
TableColumn povrsinaCol=新的TableColumn(“Povrsina”);
povrsinaCol.setCellValueFactory(cellData->
新的ReadOnlyDoubleWrapper(cellData.getValue().getPovrsina().asObject());
povrsinaCol.setCellFactory(tc->new TableCell(){
@凌驾
受保护的void updateItem(数值,布尔空){
super.updateItem(值,空);
if(空){
setText(空);
}否则{
setText(String.format(“%0.2f”,value.doubleValue());
}
}
});

你好。这可能是对已接受答案的建议编辑。