gpt4 book ai didi

javafx - 将货币符号添加到 TableView 但在编辑单元格上删除

转载 作者:行者123 更新时间:2023-12-01 11:27:37 25 4
gpt4 key购买 nike

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

有人能帮我举个基本的例子吗?我需要使用 OnEditStart 事件吗?

最佳答案

任何时候您想要配置单元格中项目的显示方式而不更改实际数据,您应该使用自定义 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);
}
}

关于javafx - 将货币符号添加到 TableView 但在编辑单元格上删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36087968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com