Editing a Number cell in a TableView

I have a TableView control that consists of several columns containing different types, including String and Number s. I am trying to write a suitable callback function for the Number cell being edited, but I canโ€™t run into anything with it, since I get a lot of problems, starting from empty cells and ending with exceptions.

I read http://docs.oracle.com/javafx/2/ui_controls/table-view.htm , but this only applies to String values โ€‹โ€‹in cells. The sticking point seems lastNameCol.setCellFactory(TextFieldTableCell.forTableColumn()); . This line appears to be for Text fields, not Number fields.

In addition, I want to check the entered numbers. Does this require a special callback for CellFactory to do this? If so, how can I develop a callback that accepts number types and validates them?

Here is the code snippet of what I have in my project:

 @FXML private TableView<BMIRecord> fxBMITable; @FXML private TableColumn<BMIRecord, String> fxBMITableDate; @FXML private TableColumn<BMIRecord, String> fxBMITableTime; @FXML private TableColumn<BMIRecord, Number> fxBMITableHeight; @FXML private TableColumn<BMIRecord, Number> fxBMITableWeight; @FXML private TableColumn<BMIRecord, Number> fxBMITableBMI; // ... private void someFunc() { fxBMITable.setEditable(true); /* BMI table callback configuration */ fxBMITableHeight.setCellValueFactory(new Callback<CellDataFeatures<BMIRecord, String>, ObservableValue<String>>() { public ObservableValue<String> call(CellDataFeatures<BMIRecord, String> p) { return new SimpleStringProperty(p.getValue().getDateString()); } }); /* * ERROR: * The method setCellFactory(Callback<TableColumn<BMIRecord,Number>,TableCell<BMIRecord,Number>>) * in the type TableColumn<BMIRecord,Number> is not applicable for the arguments * (Callback<TableColumn<Object,String>,TableCell<Object,String>>) */ fxBMITableHeight.setCellFactory(TextFieldTableCell.forTableColumn()); fxBMITableHeight.setOnEditCommit(new EventHandler<CellEditEvent<BMIRecord, Number>>() { @Override public void handle(CellEditEvent<BMIRecord, Number> t) { ((BMIRecord)t.getTableView().getItems().get(t.getTablePosition().getRow())).setHeight(t.getNewValue().doubleValue()); } }); } 

Thanks for any help in advance.

+7
java javafx tableview
source share
3 answers

TextFieldTableCell is parameterized by type and has a stringConverter property that can be used to convert to / from String and the desired type.

Try something like:

 TextFieldTableCell.<BMIRecord, Number>forTableColumn(new NumberStringConverter()) 

NumberStringConverter has several additional constructors for specifying formatting, see javadocs.

Here is a more complete example:

 public class Person { public Person(String name0, int age0) { name = name0; age = age0; } public String name; public int age; } TableView<Person> personTable = new TableView<>(); TableColumn<Person, Number> age = new TableColumn<>(); age.setCellValueFactory(new Callback<CellDataFeatures<Person, Number>, ObservableValue<Number>>() { @Override public ObservableValue<Number> call(CellDataFeatures<Person, Number> p) { return new SimpleIntegerProperty(p.getValue().age); } }); age.setCellFactory(TextFieldTableCell.<Person, Number>forTableColumn(new NumberStringConverter())); 

This doesnโ€™t work very well, because the NumberStringConverter is so dumb, poorly implemented that it just throws a ParseException at you if you need to enter a string instead of a number in the cell.

However, it should be relatively trivial to implement your own string converter, where you can also perform some simple check (for example, the value should be from 0 to 100).

+14
source share

use this

 age.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter())); 
0
source share

jalvafx library:

The solution is to prevent the user from typing incorrectly. Only 0-9, coma and dot symbols allowed. You can set the minimum and maximum value of the number.

enter image description here

How to use the signature:

 TableViewUtils.setEditNumberCellFactory( TableColumn<T, K> cell, BiConsumer<T, Double> changeHandler, String editControlHeader, Double minAllowedValue, Double maxAllowedValue, Function<T, Boolean> ignoreEdit) 

Table layout:

 TableView<String> tableView = new TableView<>(); tableView.getItems().addAll("USA", "Canada", "Brazil"); TableColumn<String, String> country = new TableColumn<>("Country"); TableColumn<String, Integer> column = new TableColumn<>("Some value"); tableView.getColumns().add(country); tableView.getColumns().add(column); country.setCellValueFactory( data -> new SimpleObjectProperty<>(data.getValue())); column.setCellValueFactory( data -> new SimpleObjectProperty<>(800)); 

Decision:

 BiConsumer<String, Double> changeHandler = (item, newValue) -> { System.out.println("New value is " + newValue); // Your code }; Function<String, Boolean> ignoreEdit = item -> item.equalsIgnoreCase("USA"); TableViewUtils.setEditNumberCellFactory(column, changeHandler, "New value:", 200.0, 800.0, ignoreEdit); 
0
source share

All Articles