1
votes

I learn Using JavaFX UI Controls Example 12-12 Adding Map Data to the Table

Its works in java:

TableColumn<Map, String> firstDataColumn = new TableColumn<>("Class A");
TableColumn<Map, String> secondDataColumn = new TableColumn<>("Class B");

firstDataColumn.setCellValueFactory(new MapValueFactory(Column1MapKey));
firstDataColumn.setMinWidth(130);
secondDataColumn.setCellValueFactory(new MapValueFactory(Column2MapKey));
secondDataColumn.setMinWidth(130);

TableView table_view = new TableView<>(generateDataInMap());

table_view.setEditable(true);
table_view.getSelectionModel().setCellSelectionEnabled(true);
table_view.getColumns().setAll(firstDataColumn, secondDataColumn);
Callback<TableColumn<Map, String>, TableCell<Map, String>>
    cellFactoryForMap = new Callback<TableColumn<Map, String>,
        TableCell<Map, String>>() {
            @Override
            public TableCell call(TableColumn p) {
                return new TextFieldTableCell(new StringConverter() {
                    @Override
                    public String toString(Object t) {
                        return t.toString();
                    }
                    @Override
                    public Object fromString(String string) {
                        return string;
                    }                                    
                });
            }
};
firstDataColumn.setCellFactory(cellFactoryForMap);
secondDataColumn.setCellFactory(cellFactoryForMap);

But this part does not work in kotlin(after converting):

     val cellFactoryForMap = 
object : Callback<TableColumn<Map<*, *>, String>, TableCell<Map<*, *>, String>> {
                override fun call(p: TableColumn<*, *>): TableCell<*, *> {
                    return TextFieldTableCell(object : StringConverter() {
                        override fun toString(t: Any): String {
                            return t.toString()
                        }

                        override fun fromString(string: String): Any {
                            return string
                        }
                    })
                }
            }

Error:(49, 33) Kotlin: Object must be declared abstract or implement abstract member public abstract fun call(p0: TableColumn>, String>!): TableCell, *>, String>! defined in javafx.util.Callback

Error:(50, 13) Kotlin: 'call' overrides nothing

Error:(51, 24) Kotlin: Type inference failed: constructor TextFieldTableCell(p0: StringConverter!) cannot be applied to ()

Error:(51, 43) Kotlin: Type mismatch: inferred type is but StringConverter<(???..???)>! was expected

Error:(51, 52) Kotlin: One type argument expected for class StringConverter defined in javafx.util

Error:(52, 21) Kotlin: 'toString' overrides nothing

Error:(56, 21) Kotlin: 'fromString' overrides nothing

How write the correct code?

1

1 Answers

4
votes

Your type is wrong in TextFieldTableCell.

closely look at TextFieldTableCell<S,T> extends TableCell<S,T> and the method TextFieldTableCell(StringConverter<T> converter)

if the callback return type is TableCell<Map<*, *>, String>>, then your string converter MUST be StringConverter<String> to fulfil the type constraint.

   Callback<TableColumn<Map<*, *>, String>, TableCell<Map<*, *>, String>> {
        TextFieldTableCell(object: StringConverter<String>() {
            override fun fromString(string: String): String {
                return string
            }

            override fun toString(`object`: String): String {
                return `object`
            }
        })
    }