【问题标题】:"Accounting" Style Table Cell in JavaFXJavaFX 中的“会计”样式表单元格
【发布时间】:2018-07-11 04:09:13
【问题描述】:

有没有办法在 JavaFX 表中创建“会计”样式的单元格? 通过会计我的意思是在单元格中让美元符号左对齐并且值右对齐。这是在 Excel 中的样子:

这是我目前尝试过的:

public class PriceTableCell<S> extends TableCell<S, Long>
{
    public PriceTableCell()
    {
        final Label label = new Label("$");
        this.setAlignment(Pos.CENTER_RIGHT);
        this.setContentDisplay(ContentDisplay.LEFT);
        this.setGraphic(label);
    }

    @Override
    protected void updateItem(Long item, boolean empty)
    {
        if (item == null || empty)
        {
            this.setText(null);
            return;
        }

        this.setText(String.format(Locale.ENGLISH, "%,d.%02d", item / 100, Math.abs(item % 100)));
    }
}

不幸的是,我没有找到一种方法来为图形和文本设置单独的对齐方式。 JavaFX 将上述内容呈现如下:

【问题讨论】:

  • 与对齐问题无关:作为一般规则,永远不要进行任何手动数字/货币格式化,而是使用 NumberFormat 使其与区域设置无关

标签: java javafx alignment tablecell


【解决方案1】:

AnchorPane 中使用两个标签应该可以工作。

(更新:根据@kleopatra 的建议,我在此解决方案中加入了DecimalFormat,它将(至少部分)本地化货币符号以及小数位数等。这将使假设货币符号显示在货币价值的左侧,这并不一定适用于所有货币,但无论如何该假设在问题中都有些隐含。)

public class PriceTableCell<S> extends TableCell<S, Long> {

    private final AnchorPane pane ;
    private final Label valueLabel ;
    // locale-aware currency format to use for formatting
    private DecimalFormat format;

    public PriceTableCell() {
        // grab an instance
        format = (DecimalFormat) NumberFormat.getCurrencyInstance();
        //get the currency symbol
        String symbol = format.getCurrency().getSymbol();
        // replace the currency symbol with an empty string
        DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
        symbols.setCurrencySymbol("");
        format.setDecimalFormatSymbols(symbols);

        Label currencySignLabel = new Label(symbol);
        valueLabel = new Label();
        pane = new AnchorPane(currencySignLabel, valueLabel);
        AnchorPane.setLeftAnchor(currencySignLabel, 0.0);
        AnchorPane.setRightAnchor(valueLabel, 0.0);
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    }

    @Override
    protected void updateItem(Long price, boolean empty) {
        super.updateItem(price, empty);
        if (empty) {
            setGraphic(null);
        } else {
            // manual formatting 
            //String text = String.format("%,d.%02d", price / 100, Math.abs(price % 100));
            valueLabel.setText(format.format(price));
            setGraphic(pane);
        }
    }
}

这是一个 SSCCE:

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Random;
import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class TableViewWithAccountingStyleCell extends Application {

    public static class PriceTableCell<S> extends TableCell<S, Long> {

        private final AnchorPane pane ;
        private final Label valueLabel ;
        // locale-aware currency format to use for formatting
        private DecimalFormat format;

        public PriceTableCell() {
            // grab an instance
            format = (DecimalFormat) NumberFormat.getCurrencyInstance();
            //get the currency symbol
            String symbol = format.getCurrency().getSymbol();
            // replace the currency symbol with an empty string
            DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
            symbols.setCurrencySymbol("");
            format.setDecimalFormatSymbols(symbols);

            Label currencySignLabel = new Label(symbol);
            valueLabel = new Label();
            pane = new AnchorPane(currencySignLabel, valueLabel);
            AnchorPane.setLeftAnchor(currencySignLabel, 0.0);
            AnchorPane.setRightAnchor(valueLabel, 0.0);
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        }

        @Override
        protected void updateItem(Long price, boolean empty) {
            super.updateItem(price, empty);
            if (empty) {
                setGraphic(null);
            } else {
                // manual formatting 
                //String text = String.format("%,d.%02d", price / 100, Math.abs(price % 100));
                valueLabel.setText(format.format(price));
                setGraphic(pane);
            }
        }
    }

    public static class Item {
        private final StringProperty name = new SimpleStringProperty();
        private final LongProperty price = new SimpleLongProperty();

        public Item(String name, long price) {
            setName(name);
            setPrice(price);
        }

        public StringProperty nameProperty() {
            return name ;
        }

        public final String getName() {
            return nameProperty().get();
        }

        public final void setName(String name) {
            nameProperty().set(name);
        }

        public LongProperty priceProperty() {
            return price ;
        }

        public final long getPrice() {
            return priceProperty().get();
        }

        public final void setPrice(long price) {
            priceProperty().set(price);
        }
    }

    @Override
    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>();
        table.getColumns().add(column("Item", Item::nameProperty));
        TableColumn<Item, Long> priceColumn = column("Price", item -> item.priceProperty().asObject());
        priceColumn.setPrefWidth(300);

        priceColumn.setCellFactory(tc -> new PriceTableCell<>());

        table.getColumns().add(priceColumn);


        Random rng = new Random();
        for (int i = 1 ; i <= 20 ; i++) {
            table.getItems().add(new Item("Item "+i, rng.nextInt(1_000_000)));
        }

        Scene scene = new Scene(table);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private <S,T> TableColumn<S,T> column(String name, Function<S, ObservableValue<T>> property) {
        TableColumn<S,T> column = new TableColumn<>(name);
        column.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        return column ;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

产生

【讨论】:

  • 使用两个标签和 DecimalFormat 将使其独立于区域设置:)
  • @kleopatra 我打算去那里,然后开始想知道您是否可以确定货币符号相对于数字部分的位置(例如,iirc 意大利里拉的符号,位于数字之后),然后想知道 Excel 对这些货币做了什么(如果它们仍然存在的话),然后我放弃并复制了 OP 的格式.. ;)。
  • 是的,这也让我思考 :) 看到我对你的答案的补充并将其纳入你的答案(然后我会删除我的)
  • 精彩的回答,谢谢!当单元格变得太小时时,“$”会进入值,这很遗憾。相反,使用 BorderPane 似乎可以缓解这种情况。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 2012-09-24
  • 2015-02-09
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多