javafx中root的invoke方法报错Method too large问题记录

问题描述
  在使用javafx绘制PC桌面程序页面时,使用到了tableview,tableview可以很方便的维护一个可编辑的表,简易代码如下。

	var bomList = observableListOf<BomContent.BomsInfo>()
	tableview(bomList) {
		column("客户名称", BomContent.BomsInfo::bomID) {
         	isEditable = true
            prefWidth = 150.0
            isSortable = true
        }.makeEditable()
    }

  具体效果如下
在这里插入图片描述
  这其中有一个方法.makeEditable()方法,其源码如下

inline fun <T, reified S : Any> TableColumn<T, S>.makeEditable() = apply {
    tableView?.isEditable = true
    isEditable = true
    when (S::class.javaPrimitiveType ?: S::class) {
        Int::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(IntegerStringConverter() as StringConverter<S>)
        Integer::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(IntegerStringConverter() as StringConverter<S>)
        Integer::class.javaPrimitiveType -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(IntegerStringConverter() as StringConverter<S>)
        Double::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(DoubleStringConverter() as StringConverter<S>)
        Double::class.javaPrimitiveType -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(DoubleStringConverter() as StringConverter<S>)
        Float::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(FloatStringConverter() as StringConverter<S>)
        Float::class.javaPrimitiveType -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(FloatStringConverter() as StringConverter<S>)
        Long::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(LongStringConverter() as StringConverter<S>)
        Long::class.javaPrimitiveType -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(LongStringConverter() as StringConverter<S>)
        Number::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(NumberStringConverter() as StringConverter<S>)
        BigDecimal::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(BigDecimalStringConverter() as StringConverter<S>)
        BigInteger::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(BigIntegerStringConverter() as StringConverter<S>)
        String::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(DefaultStringConverter() as StringConverter<S>)
        LocalDate::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(LocalDateStringConverter() as StringConverter<S>)
        LocalTime::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(LocalTimeStringConverter() as StringConverter<S>)
        LocalDateTime::class -> cellFactory = TextFieldTableCell.forTableColumn<T, S>(LocalDateTimeStringConverter() as StringConverter<S>)
        Boolean::class.javaPrimitiveType -> {
            (this as TableColumn<T, Boolean?>).useCheckbox(true)
        }
        else -> throw RuntimeException("makeEditable() is not implemented for specified class type:" + S::class.qualifiedName)
    }
}

  简而言之,就是无论我们使用到的类型为什么类型,该方法都会使表格该列可进行编辑,并适配对应类型,这在平时使用是没有问题的。但是如果是在view的root初始化时,tableview中创建的列数太多,且每一列都是调用的.makeEditable()方法,该view的root在调用invoke方法时就会报错Method too large。

解决方案
  不使用column自带的方法.makeEditable(),根据使用的类型,进行单个编写,如下。

	var bomList = observableListOf<BomContent.BomsInfo>()
	tableview(bomList) {
		column("客户名称", BomContent.BomsInfo::bomID) {
         	isEditable = true
            prefWidth = 150.0
            isSortable = true
            cellFactory = TextFieldTableCell.forTableColumn<BomContent.BomsInfo, String>(DefaultStringConverter() as StringConverter<String>)
        }
    }

这里我们bomID使用到的是String类型。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值