JavaFX格式在TableColumn中加倍

时间:2019-01-14 19:31:33

标签: java javafx

我已经查看并且无法弄清楚如何使用Double格式化TableColumn。它总是以234.23232033等结尾,例如,我想显示234.23。我看到过类似我的文章,但我只是想不出如何将其集成到我的代码中。我的部分过错也许是因为我远远超出了我的实际了解。但我想弄清楚这一点。谢谢。

这是我的控制器的一部分...

@FXML
private TableView<CheckBookData> tableView;
@FXML
private TableColumn<CheckBookData, String> transactionCol;
@FXML
private TableColumn<CheckBookData, Double> balanceCol;

ObservableList<CheckBookData> checkbook = 
FXCollections.observableArrayList();

Double balance = 0.0;

public void initialize(URL url, ResourceBundle rb)
{
    transactionCol.setCellValueFactory(new 
    PropertyValueFactory<CheckBookData, String>("transaction"));

    balanceCol.setCellValueFactory(new PropertyValueFactory<CheckBookData, 
            Double>("balance"));
}

private void handleAddData(ActionEvent event)
{
    Double deposit = Double.parseDouble(depositField.getText());

    balance = balance + deposit;

    checkbook.add(new CheckBookData(transaction, withdraw, deposit, 
    balance, checkNumber, date));
}

这是我的CheckBookData类的一部分...

public class CheckBookData
{
private SimpleStringProperty checkNumber, transaction, date;
private SimpleDoubleProperty withdraw, deposit, balance;

public CheckBookData(String transaction, Double withdraw, Double deposit,
        Double balance, String checkNumber, String date)
{
    this.transaction = new SimpleStringProperty(transaction);
    this.withdraw = new SimpleDoubleProperty(withdraw);
    this.deposit = new SimpleDoubleProperty(deposit);
    this.balance = new SimpleDoubleProperty(balance);
    this.checkNumber = new SimpleStringProperty(checkNumber);
    this.date = new SimpleStringProperty(date);
}
public Double getBalance()
{
    return balance.get();
}

public void setBalance(SimpleDoubleProperty balance)
{
    this.balance = balance;
}

1 个答案:

答案 0 :(得分:0)

您必须设置值的格式,而不是表格的格式-只需清除即可。
您可以对这个问题应用一些数学方法:

[@TabName]

您将其乘以100,将其四舍五入,然后除以100。这将为您提供格式化的结果。