Javafx 将货币符号添加到tableview,但在编辑单元格时删除
我已经知道如何使用表单元格工厂回调操作表单元格。我在单元格中添加了一个货币符号,使其看起来整洁。(即5,00欧元而不是5,00欧元)问题是,当我双击单元格时,我希望删除该符号。但更糟糕的是,我无法找到如何再次操纵文本字段来删除该货币符号,并在用户提交编辑时将其带回。基本上,我在Excel中编辑单元格时尝试做的是类似的事情:)Javafx 将货币符号添加到tableview,但在编辑单元格时删除,javafx,Javafx,我已经知道如何使用表单元格工厂回调操作表单元格。我在单元格中添加了一个货币符号,使其看起来整洁。(即5,00欧元而不是5,00欧元)问题是,当我双击单元格时,我希望删除该符号。但更糟糕的是,我无法找到如何再次操纵文本字段来删除该货币符号,并在用户提交编辑时将其带回。基本上,我在Excel中编辑单元格时尝试做的是类似的事情:) 有没有人能帮我举个简单的例子?是否需要使用OnEditStart事件?任何时候,如果要配置单元格中项目的显示方式,而不更改实际数据,则应使用自定义的表格单元格。下面是一个展
有没有人能帮我举个简单的例子?是否需要使用OnEditStart事件?任何时候,如果要配置单元格中项目的显示方式,而不更改实际数据,则应使用自定义的
表格单元格。下面是一个展示您想要的行为的示例:
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);
}
}
导入java.text.DecimalFormat;
导入java.text.DecimalFormatSymbols;
导入java.text.NumberFormat;
导入java.text.ParseException;
导入java.util.function.UnaryOperator;
导入javafx.scene.control.ContentDisplay;
导入javafx.scene.control.TableCell;
导入javafx.scene.control.TextField;
导入javafx.scene.control.TextFormatter;
导入javafx.scene.control.TextFormatter.Change;
导入javafx.scene.input.KeyCode;
导入javafx.scene.input.KeyEvent;
导入javafx.util.StringConverter;
公共类CurrencyCell扩展了TableCell{
私有最终文本字段文本字段;
private final NumberFormat format=DecimalFormat.getCurrencyInstance();
私有最终DecimalFormat textFieldFormat=新DecimalFormat(“0.00”);
公共货币单元(){
this.textField=新的textField();
StringConverter转换器=新的StringConverter(){
@凌驾
公共字符串toString(双对象){
返回对象==null?”:textFieldFormat.format(对象);
}
@凌驾
public Double fromString(字符串字符串){
试一试{
返回string.isEmpty()?0.0:textFieldFormat.parse(string.doubleValue();
}捕获(解析异常){
e、 printStackTrace();
返回0.0;
}
}
};
一元运算符筛选器=(更改)->{
字符串newText=change.getControlNewText();
if(newText.isEmpty()){
回报变化;
}
试一试{
textFieldFormat.parse(newText);
回报变化;
}捕获(解析异常exc){
返回null;
}
};
TextFormatter TextFormatter=新的TextFormatter(转换器,0.0,过滤器);
setTextFormatter(textFormatter);
setOnAction(e->committedit(converter.fromString(textField.getText()));
textField.addEventFilter(按下KeyEvent.KEY,e->{
if(例如getCode()==KeyCode.ESCAPE){
取消编辑();
}
});
设置图形(文本字段);
setContentDisplay(仅限ContentDisplay.TEXT_);
}
@凌驾
受保护的void updateItem(双项,布尔空){
super.updateItem(项,空);
if(空){
setText(空);
setContentDisplay(仅限ContentDisplay.TEXT_);
}else if(isEditing()){
textField.setText(item.toString());
setContentDisplay(仅限ContentDisplay.GRAPHIC_);
}否则{
setText(format.format(item));
setContentDisplay(仅限ContentDisplay.TEXT_);
}
}
@凌驾
公开作废已启动IT(){
super.startEdit();
setText(textFieldFormat.format(getItem());
setContentDisplay(仅限ContentDisplay.GRAPHIC_);
textField.requestFocus();
textField.selectAll();
}
@凌驾
公共作废取消编辑(){
super.cancelEdit();
setText(format.format(getItem());
setContentDisplay(仅限ContentDisplay.TEXT_);
}
@凌驾
public void committedit(双倍新值){
super.committedit(newValue);
setContentDisplay(仅限ContentDisplay.TEXT_);
}
}
下面是一个使用它的示例:
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);
}
}
import java.util.Locale;
导入java.util.Random;
导入java.util.function.function;
导入javafx.application.application;
导入javafx.beans.property.DoubleProperty;
导入javafx.beans.property.property;
导入javafx.beans.property.SimpleDoubleProperty;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.scene.scene;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.stage.stage;
公共类CurrencyCellTest扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
TableView table=新TableView();
table.setEditable(true);
table.getColumns().add(列(“项”,项::nameProperty));
TableColumn priceCol=列(“价格”,项目->项目.priceProperty().asObject());
table.getColumns().add(priceCol);
priceCol.setCellFactory(tc->new CurrencyCell());
随机rng=新随机();
for(int i=1;i property.apply(cellData.getValue());
返回列;
}
公共静态类项{
私有最终StringProperty名称=新SimpleStringProperty();
私人最终双倍房地产价格=新SimpleDoubleProperty();
公共项目(字符串名称,双倍价格){
集合名(名称);
设定价格(价格);
}
公共最终字符串属性nameProperty(){
返回此.name;
}
公共最终字符串getName(){
返回此.nameProperty().get();
}
公共最终void集合名(最终字符串名){
this.nameProperty().set(name);
}
公共财产