将货币符号添加到tableview但在编辑单元格中删除

时间:2016-03-18 15:14:07

标签: javafx

我已经知道如何使用表格单元工厂回调来操作表格单元格。我在单元格中添加了一个货币符号,使其看起来整洁。 (即€5,00而不是5,00)事情是,当我双击单元格时,我希望删除该符号。但是对于它,我无法找到我如何能够再次操作文本字段以删除该货币符号并在用户提交编辑时将其重新输入。基本上我在Excel中编辑单元格时尝试做的事情类似:)。

有人可以用一个基本的例子来帮助我吗?我是否需要使用OnEditStart事件?

1 个答案:

答案 0 :(得分:5)

如果您想要配置单元格中的项目的显示方式而不更改实际数据,则应使用自定义TableCell。这是一个展示您想要的行为的示例:

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.function.UnaryOperator;

import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.util.StringConverter;

public class CurrencyCell<T> extends TableCell<T, Double> {

    private final TextField textField ;

    private final NumberFormat format = DecimalFormat.getCurrencyInstance();
    private final DecimalFormat textFieldFormat = new DecimalFormat("0.00");

    public CurrencyCell() {
        this.textField = new TextField();
        StringConverter<Double> converter = new StringConverter<Double>() {

            @Override
            public String toString(Double object) {
                return object == null ? "" : textFieldFormat.format(object) ;
            }

            @Override
            public Double fromString(String string) {
                try {
                    return string.isEmpty() ? 0.0 : textFieldFormat.parse(string).doubleValue();
                } catch (ParseException e) {
                    e.printStackTrace();
                    return 0.0 ;
                }
            }

        };
        UnaryOperator<Change> filter = (Change change) -> {
            String newText = change.getControlNewText() ;
            if (newText.isEmpty()) {
                return change ;
            }
            try {
                textFieldFormat.parse(newText);
                return change ;
            } catch (ParseException exc) {
                return null ;
            }
        };
        TextFormatter<Double> textFormatter = new TextFormatter<Double>(converter, 0.0, filter);
        textField.setTextFormatter(textFormatter);

        textField.setOnAction(e -> commitEdit(converter.fromString(textField.getText())));
        textField.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
            if (e.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
            }
        });

        setGraphic(textField);
        setContentDisplay(ContentDisplay.TEXT_ONLY);

    }

    @Override
    protected void updateItem(Double item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setText(null);
            setContentDisplay(ContentDisplay.TEXT_ONLY);
        } else if (isEditing()) {
            textField.setText(item.toString());
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        } else {
            setText(format.format(item));
            setContentDisplay(ContentDisplay.TEXT_ONLY);
        }
    }

    @Override
    public void startEdit() {
        super.startEdit();
        textField.setText(textFieldFormat.format(getItem()));
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        textField.requestFocus();
        textField.selectAll();
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();
        setText(format.format(getItem()));
        setContentDisplay(ContentDisplay.TEXT_ONLY);
    }

    @Override
    public void commitEdit(Double newValue) {
        super.commitEdit(newValue);
        setContentDisplay(ContentDisplay.TEXT_ONLY);
    }
}

这是一个使用它的例子:

import java.util.Locale ;
import java.util.Random;
import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class CurrencyCellTest extends Application {

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

        priceCol.setCellFactory(tc -> new CurrencyCell<>());

        Random rng = new Random();
        for (int i = 1; i <= 100; i++) {
            table.getItems().add(new Item("Item "+i, rng.nextInt(10000)/100.0));
        }

        primaryStage.setScene(new Scene(table, 600, 600));
        primaryStage.show();
    }

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

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

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

        public final StringProperty nameProperty() {
            return this.name;
        }


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


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


        public final DoubleProperty priceProperty() {
            return this.price;
        }


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


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



    }

    public static void main(String[] args) {
        // for testing:
        Locale.setDefault(new Locale("NL", "nl"));
        launch(args);
    }
}
相关问题