spawpaw / mybatis-generator-gui-extension

图形化MBG,内置丰富插件,可生成Service、Controller、View,配置简单。 A powerful GUI tool for MyBatisGenerator(MBG)
Other
642 stars 233 forks source link

表格字段编辑回车生效后,有垂直滚动条时,会出现编辑单元格丢失 #31

Closed sdtm1016 closed 5 years ago

sdtm1016 commented 5 years ago

表格字段编辑回车生效后,有垂直滚动条时,当滚动到看不到,再滚动回时,会出现编辑单元格丢失

spawpaw commented 5 years ago

刚试了一下,没有这个问题呀..

是不是再次编辑时单元格原有的内容会被清空?这个是javafx的锅..要是是这个问题的话就先将就着用吧,暂时没有好的解决办法

sdtm1016 commented 5 years ago

我发现这样好像可以

package com.spawpaw.mybatis.generator.gui.controller;
import com.alibaba.fastjson.JSON;
import com.spawpaw.mybatis.generator.gui.entity.TableColumnMetaData;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.Initializable;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.ScrollEvent;
import javafx.util.converter.DefaultStringConverter;
import org.apache.commons.lang.StringUtils;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * Created By spawpaw@hotmail.com  2018-01-31
 *
 * @author BenBenShang spawpaw@hotmail.com
 */
public class TableColumnEditorController extends BaseController implements Initializable {

    //    public TextField tf_filter;
    public TableView<TableColumnMetaData> table_view;
    public TableColumn<TableColumnMetaData, Boolean> c_checked;
    public TableColumn<TableColumnMetaData, String> c_column_name;
    public TableColumn<TableColumnMetaData, String> c_jdbc_type;
    public TableColumn<TableColumnMetaData, String> c_java_type;
    public TableColumn<TableColumnMetaData, String> c_property_name;
    public TableColumn<TableColumnMetaData, String> c_type_handler;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        c_checked.setCellValueFactory(new PropertyValueFactory<>("checked"));
        c_column_name.setCellValueFactory(new PropertyValueFactory<>("columnName"));
        c_jdbc_type.setCellValueFactory(new PropertyValueFactory<>("jdbcType"));
        c_property_name.setCellValueFactory(new PropertyValueFactory<>("propertyName"));
        c_type_handler.setCellValueFactory(new PropertyValueFactory<>("typeHandler"));

        c_checked.setCellFactory(CheckBoxTableCell.forTableColumn(c_checked));

//=======================下面这一部分=============================
       c_java_type.setCellFactory(column -> new TextFieldTableCell<TableColumnMetaData, String>(new DefaultStringConverter()) {
            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                TableRow<TableColumnMetaData> currentRow = getTableRow();
                TableColumnMetaData currentItem = currentRow.getItem();
                if (currentItem == null) {
                    return;
                }
                System.out.println("currentJavaTypeItem:\t" + JSON.toJSONString(currentItem));
                String javaType = currentItem.getJavaType();
                if (StringUtils.isNotEmpty(javaType)) {
                    super.setText(javaType);
                }
            }
        });
        c_property_name.setCellFactory(column -> new TextFieldTableCell<TableColumnMetaData, String>(new DefaultStringConverter()) {
            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                TableRow<TableColumnMetaData> currentRow = getTableRow();
                TableColumnMetaData currentItem = currentRow.getItem();
                if (currentItem == null) {
                    return;
                }
                System.out.println("currentPropertyItem:\t" + JSON.toJSONString(currentItem));
                String propertyName = currentItem.getPropertyName();
                if (StringUtils.isNotEmpty(propertyName)) {
                    super.setText(propertyName);
                }
            }
        });
        c_type_handler.setCellFactory(column -> new TextFieldTableCell<TableColumnMetaData, String>(new DefaultStringConverter()) {
            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                TableRow<TableColumnMetaData> currentRow = getTableRow();
                TableColumnMetaData currentItem = currentRow.getItem();
                if (currentItem == null) {
                    return;
                }
                System.out.println("currentTypeHandlerItem:\t" + JSON.toJSONString(currentItem));
                String typeHandler = currentItem.getTypeHandler();
                if (StringUtils.isNotEmpty(typeHandler)) {
                    super.setText(typeHandler);
                }
            }
        });
  // ======================上面这一部分==================================
        c_java_type.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableColumnMetaData, String>>() {
            @Override
            public void handle(TableColumn.CellEditEvent<TableColumnMetaData, String> event) {
                System.out.println("++++c_java_type++++");
                ObservableList<TableColumnMetaData> itemList = event.getTableView().getItems();
                System.out.println("++++old itemList:\n" + JSON.toJSONString(itemList));
                int index = event.getTablePosition().getRow();
                TableColumnMetaData metaData = itemList.get(index);
                String newValue = event.getNewValue();
                String oldValue = metaData.getJavaType();
                metaData.setJavaType(newValue);
                System.out.println("++++index: " + index);
                System.out.println("++++oldValue: " + oldValue);
                System.out.println("++++newValue: " + newValue);
                System.out.println("++++new itemList:\n" + JSON.toJSONString(itemList));
            }
        });
        c_property_name.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableColumnMetaData, String>>() {
            @Override
            public void handle(TableColumn.CellEditEvent<TableColumnMetaData, String> event) {
                System.out.println("++++c_property_name++++");
                event.getTableView().getItems().get(event.getTablePosition().getRow()).setPropertyName(event.getNewValue());
            }
        });
        c_type_handler.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableColumnMetaData, String>>() {
            @Override
            public void handle(TableColumn.CellEditEvent<TableColumnMetaData, String> event) {
                System.out.println("++++c_type_handler++++");
                event.getTableView().getItems().get(event.getTablePosition().getRow()).setTypeHandler(event.getNewValue());
            }
        });
    }

    public void on_btn_confirm_clicked(ActionEvent actionEvent) {

        ObservableList<TableColumnMetaData> itemList = this.table_view.getItems();
        System.out.println("++++on_btn_confirm_clicked:\n" + JSON.toJSONString(itemList));
        tableColumnEditorStage.close();
    }

    public void setColumns(List<TableColumnMetaData> tableColumnMetaDataList) {
        table_view.setItems(FXCollections.observableList(tableColumnMetaDataList));
    }

}