&引用;“会计”;JavaFX中的样式表单元格

&引用;“会计”;JavaFX中的样式表单元格,java,javafx,alignment,tablecell,Java,Javafx,Alignment,Tablecell,有没有办法在JavaFX表中创建“记帐”样式的单元格? 通过会计,我的意思是让美元符号在单元格中左对齐,值在单元格中右对齐。以下是Excel中的外观: 以下是我迄今为止所做的尝试: public class PriceTableCell<S> extends TableCell<S, Long> { public PriceTableCell() { final Label label = new Label("$");

有没有办法在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)));
    }
}
公共类PriceTableCell扩展了TableCell
{
公共价格表单元格()
{
最终标签=新标签($);
此.设置对齐(位置居中\右侧);
this.setContentDisplay(ContentDisplay.LEFT);
本.设置图形(标签);
}
@凌驾
受保护的void updateItem(长项,布尔值为空)
{
如果(项==null | |空)
{
this.setText(null);
返回;
}
this.setText(String.format(Locale.ENGLISH,%,d.%02d),item/100,Math.abs(item%100));
}
}
不幸的是,我没有找到一种为图形和文本设置单独对齐的方法。JavaFX呈现上述内容如下:


锚烷中使用两个标签应该有效

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

公共类PriceTableCell扩展了TableCell{
私人最终锚烷窗格玻璃;
私人最终标签valueLabel;
//用于格式化的区域设置感知的货币格式
专用十进制格式;
公共价格表单元格(){
//抓住一个例子
格式=(DecimalFormat)NumberFormat.getCurrencyInstance();
//获取货币符号
字符串符号=format.getCurrency().getSymbol();
//将货币符号替换为空字符串
DecimalFormatSymbols symbols=format.getDecimalFormatSymbols();
符号。设置货币符号(“”);
格式。setDecimalFormatSymbols(符号);
标签电流SignLabel=新标签(符号);
valueLabel=新标签();
窗格=新的锚烷(currencySignLabel,valueLabel);
AnchorPane.setLeftAnchor(currencySignLabel,0.0);
AnchorPane.setRightAnchor(值标签,0.0);
setContentDisplay(仅限ContentDisplay.GRAPHIC_);
}
@凌驾
受保护的void updateItem(长价格,布尔空){
super.updateItem(价格,空);
if(空){
设置图形(空);
}否则{
//手动格式化
//String text=String.format(“%,d.%02d”,price/100,Math.abs(price%100));
valueLabel.setText(format.format(price));
设置图形(窗格);
}
}
}
以下是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);
    }
}
导入java.text.DecimalFormat;
导入java.text.DecimalFormatSymbols;
导入java.text.NumberFormat;
导入java.util.Random;
导入java.util.function.function;
导入javafx.application.application;
导入javafx.beans.property.LongProperty;
导入javafx.beans.property.SimpleLongProperty;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.beans.value.observeValue;
导入javafx.scene.scene;
导入javafx.scene.control.ContentDisplay;
导入javafx.scene.control.Label;
导入javafx.scene.control.TableCell;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.layout.ancorpane;
导入javafx.stage.stage;
使用AccountingStyleCell扩展应用程序的公共类TableView{
公共静态类PriceTableCell扩展了TableCell{
私人最终锚烷窗格玻璃;
私人最终标签valueLabel;
//用于格式化的区域设置感知的货币格式
专用十进制格式;
公共价格表单元格(){
//抓住一个例子
格式=(DecimalFormat)NumberFormat.getCurrencyInstance();
//获取货币符号
字符串符号=format.getCurrency().getSymbol();
//将货币符号替换为空字符串
DecimalFormatSymbols symbols=format.getDecimalFormatSymbols();
符号。设置货币符号(“”);
格式。setDecimalFormatSymbols(符号);
标签电流SignLabel=新标签(符号);
valueLabel=新标签();
窗格=新的锚烷(currencySignLabel,valueLabel);
AnchorPane.setLeftAnchor(currencySignLabel,0.0);
AnchorPane.setRightAnchor(值标签,0.0);
setContentDisplay(仅限ContentDisplay.GRAPHIC_);
}
@凌驾
受保护的void updateItem(长价格,布尔空){
super.updateItem(价格,空);
if(空){
设置图形(空);
}否则{
//手动格式化
//String text=String.format(“%,d.%02d”,price/100,Math.abs(price%100));
valueLabel.setText(format.format(price));
设置图形(窗格);
}
}
}
公共静态类项{
私有最终StringProperty名称=新SimpleStringProperty();
私有最终LongProperty价格=新SimpleLongProperty();
公共项目(字符串名称、长价格){
集合名(名称);
设定价格(价格);
}
公共字符串属性nameProperty(){
返回名称;
}
公共最终字符串getName(){
返回nameProperty().get();
}
公共最终无效集合名(字符串名){
nameProperty().set(名称);
}
公共物业价格物业(){
退货价格;
}
公开最终长期价格(){
返回priceProperty().get();
}
公开决赛
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);
    }
}