如何在不将其更改为字符串的情况下使用 Integer 数据类型 editable 创建 table 列

How to make a table column with Integer datatype editable without changing it to String

我有一个数据类型为整数的 table 列。我想在不将任何地方的数据类型更改为 String 的情况下创建此列 editable 。我使用了 textfieldtablecell 但它只在按下 enter 时提交值。所以我想要一些其他的方法。请提出一些建议。为了做到这一点 [可能我做了这样的事情。但是我的手机没有变成 editable.

public class EditCell implements initializable{

 @FXML
    private TableView<ResourceMaster> roletable;  
@FXML
    private TableColumn<ResourceMaster, Integer> loadedHrs;
@Override
    public void initialize(URL location, ResourceBundle resources) {

Callback<TableColumn<ResourceMaster,Integer>, TableCell<ResourceMaster,Integer>> txtCellFactory = 
                (TableColumn<ResourceMaster,Integer> p) -> {return new EditingCell();};

loadedHrs.setCellFactory(txtCellFactory);
} 

   public class EditingCell extends TableCell<ResourceMaster, Integer> {
        private TextField textField;
        @Override
        public void startEdit() {
            if (!isEmpty()) {
                super.startEdit();
                if (textField == null) {
                    createTextField();
                }
               // setText(null);
                commitEdit(Integer.valueOf((textField.getText())));
                setGraphic(textField);
                setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                textField.requestFocus();
            }
        }

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

        @Override
        public void updateItem(Integer item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                //setText(null);
                setGraphic(null);
            } else {
                if (isEditing()) {
                    if (textField != null) {
                        textField.setText(String.valueOf(getString()));
                    }
                    setGraphic(textField);
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                } else {
                    setText(String.valueOf(getString()));
                    setContentDisplay(ContentDisplay.TEXT_ONLY);
                }
            }
        }

        private void createTextField() {
            textField = new TextField(String.valueOf(getString()));
            textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
            textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent t) {
                    if (t.getCode() == KeyCode.ENTER) {
                        commitEdit(Integer.valueOf(textField.getText()));
                        EditingCell.this.getTableView().requestFocus();//why does it lose focus??
                        EditingCell.this.getTableView().getSelectionModel().selectBelowCell();
                    } else if (t.getCode() == KeyCode.ESCAPE) {
                        cancelEdit();
                    }
                }
            });

            textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent t) {
                    if (t.getCode().isDigitKey()) {
                        if (CellField.isLessOrEqualOneSym()) {
                            CellField.addSymbol(t.getText());
                        } else {
                            CellField.setText(textField.getText());
                        }
                        textField.setText(CellField.getText());
                        textField.deselect();
                        textField.end();
                        textField.positionCaret(textField.getLength() + 2);//works sometimes

                    }
                }
            });
        }

        private Integer getString() {
            return getItem();
        }
}
}

编辑时只需使用在单元格中创建文本字段的单元格工厂。要提交编辑,只需解析文本字段中的文本。

示例:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class EditableTableColumnWithInteger extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>();
        table.setEditable(true);
        table.getItems().addAll(createData());

        TableColumn<Item, String> nameCol = new TableColumn<>("Name");
        nameCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty());

        TableColumn<Item, Number> valueCol = new TableColumn<>("Value");
        valueCol.setCellValueFactory(cellData -> cellData.getValue().valueProperty());

        valueCol.setCellFactory(col -> new IntegerEditingCell());

        table.getColumns().add(nameCol);
        table.getColumns().add(valueCol);

        Button dataDumpButton = new Button("Dump data");
        dataDumpButton.setOnAction( e -> 
            table.getItems().stream().map(item -> item.getName()+":"+item.getValue()).forEach(System.out::println));
        HBox controls = new HBox(5, dataDumpButton);
        controls.setPadding(new Insets(10));
        controls.setAlignment(Pos.CENTER);

        primaryStage.setScene(new Scene(new BorderPane(table, null, null, controls, null), 600, 400));
        primaryStage.show();
    }

    public class IntegerEditingCell extends TableCell<Item, Number> {

        private final TextField textField = new TextField();
        private final Pattern intPattern = Pattern.compile("-?\d+");

        public IntegerEditingCell() {
            textField.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> {
                if (! isNowFocused) {
                    processEdit();
                }
            });
            textField.setOnAction(event -> processEdit());
        }

        private void processEdit() {
            String text = textField.getText();
            if (intPattern.matcher(text).matches()) {
                commitEdit(Integer.parseInt(text));
            } else {
                cancelEdit();
            }
        }

        @Override
        public void updateItem(Number value, boolean empty) {
            super.updateItem(value, empty);
            if (empty) {
                setText(null);
                setGraphic(null);
            } else if (isEditing()) {
                setText(null);
                textField.setText(value.toString());
                setGraphic(textField);
            } else {
                setText(value.toString());
                setGraphic(null);
            }
        }

        @Override
        public void startEdit() {
            super.startEdit();
            Number value = getItem();
            if (value != null) {
                textField.setText(value.toString());
                setGraphic(textField);
                setText(null);
            }
        }

        @Override
        public void cancelEdit() {
            super.cancelEdit();
            setText(getItem().toString());
            setGraphic(null);
        }

        // This seems necessary to persist the edit on loss of focus; not sure why:
        @Override
        public void commitEdit(Number value) {
            super.commitEdit(value);
            ((Item)this.getTableRow().getItem()).setValue(value.intValue());
        }
    }

    private List<Item> createData() {
        Random rng = new Random();
        List<Item> items = new ArrayList<>();
        for (int i=1; i<=20; i++) {
            items.add(new Item("Item "+i, rng.nextInt(20)));
        }
        return items ;
    }

    public static class Item {
        private final StringProperty name = new SimpleStringProperty();
        private final IntegerProperty value = new SimpleIntegerProperty();
        public Item(String name, int value) {
            this.setName(name);
            this.setValue(value);
        }
        public final StringProperty nameProperty() {
            return this.name;
        }
        public final java.lang.String getName() {
            return this.nameProperty().get();
        }
        public final void setName(final java.lang.String name) {
            this.nameProperty().set(name);
        }
        public final IntegerProperty valueProperty() {
            return this.value;
        }
        public final int getValue() {
            return this.valueProperty().get();
        }
        public final void setValue(final int value) {
            this.valueProperty().set(value);
        }

    }

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

您可以使用 StringConverter 创建 table 单元格,这会将您的对象转换为单元格显示。您可以为您的 class

实现自己的 StringConverter
cell.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter<AnyClass>() {

        @Override
        public String toString(AnyClass object) {
            return null;
        }

        @Override
        public AnyClass fromString(String string) {
            return null;
        }
    }));

JavaFX 还包含一些默认转换器。例如。 IntegerStringConverter 适合您的情况,

TextFieldTableCell.forTableColumn(new IntegerStringConverter())