TableView列数据设置为2位小数 [英] TableView column data set to 2 decimal places

查看:691
本文介绍了TableView列数据设置为2位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 1 class file Nepokretnost.java in which constructor look like this:

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设置。

Further, I have TableView with column for each of class fields. My problem is double field "povrsina". I want to set it from TextField.

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

I sent contents of TextField named txtPovrsina to double variable:

double dPovrsina;
dPovrsina = Double.parseDouble(txtPovrsina.getText());

然后将所有字段放在TableView中:

then put all fields in 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));

所有的工作都很好,但是我想要一些应用程序的行为,我无法弄清楚如何设置。现在,当我将Text 25中的int放在TextField中时,我在TableView列中得到25.0。我想让所有列单元格具有两个小数位。

All is working well, but I want some behavior of app I can't figure out how to set. Right now when I put int like 25 in TextField I get 25.0 in TableView column. I want all column cells to have exactly 2 decimal places.

我尝试了:

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));

但是我收到错误类型不兼容:字符串无法转换为双

but I get error "Incompatible types: String cannot be converted to double"

我在java中仍然是noob,但我的猜测是格式化了String,我想输入保留double只有2位小数。像这个专栏我将会遇到与其他双重领域相同的问题。

I am still noob in java but my guess is format is making String and I want to input stay double just to have 2 decimal places. Like this column I will have same issue with other double fields.

任何人都可以给我一些方向吗?

Can anyone give me some direction please?

推荐答案

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

You want to change the way the data is displayed in your table, not change the data itself. To do that, you need to set a cell factory on your table column. Something like

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列数据设置为2位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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