回调Lambda表达式+ JavaFX [英] Call Back Lambda Expressions + JavaFX

查看:102
本文介绍了回调Lambda表达式+ JavaFX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图更新我的旧JavaFX应用程序之一,该应用程序是在Java 6 Release上创建的.给了我一个提示,我可以转换当前代码并改用lambda表达式,有人可以帮助我在此处转换这段代码,或者以某种方式指导我吗?

So, I am trying to renew one of my old JavaFX applications which were created upon Java 6 Release. I was given a tip that I could convert this current code and use lambda expressions instead, could someone help me convert this piece of code here or somehow guide me?

// define a simple boolean cell value for the action column so that the column will only be shown for non-empty rows.
    addColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<UserDetails, Boolean>, ObservableValue<Boolean>>() {
        @Override public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<UserDetails, Boolean> features) {
            return new SimpleBooleanProperty(features.getValue() != null);
        }
    });

    // create a cell value factory with an add button for each row in the table.
    addColumn.setCellFactory(new Callback<TableColumn<UserDetails, Boolean>, TableCell<UserDetails, Boolean>>() {
        @Override public TableCell<UserDetails, Boolean> call(TableColumn<UserDetails, Boolean> personBooleanTableColumn) {
            return new AddPersonCell(window, tableUser);
        }
    });

推荐答案

Lambda表达式仅在交互中只有一个抽象方法时才起作用.由于Callback就是这种情况,因此这里可以是一个.

Lambda expressions only work, if there is a single abstract method in the interfact. Since this is the case for Callback this can be one here.

基本上,您将匿名类放置为具有(<parameters>) -> <method body>形式的lambda表达式.

Basically you place the anonymus class with a lambda expression of the form (<parameters>) -> <method body>.

在这种情况下

new Callback<TableColumn.CellDataFeatures<UserDetails, Boolean>, ObservableValue<Boolean>>() {
    @Override public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<UserDetails, Boolean> features) {
        return new SimpleBooleanProperty(features.getValue() != null);
    }
}

成为

(TableColumn.CellDataFeatures<UserDetails, Boolean> features) -> {
    return new SimpleBooleanProperty(features.getValue() != null);
}

这可以进一步简化:

  1. 如果不需要参数类型来确定要调用的方法,则可以将其删除.
  2. 如果只有一个参数没有类型,则可以删除()括号.
  3. 如果方法主体仅包含一个语句,则可以删除{};.如果该语句是return语句,则也需要删除return关键字.
  1. If the parameter types are not needed to decide the method to be called, they can be removed.
  2. If there is only a single parameter without a type, the () brackets can be removed.
  3. If the method body contains only a single statement, the {} and the ; can be removed. If the statement is a return statement, the return keyword needs to be removed too.

这使您可以进一步将lambda表达式简化为

This allows you to further simplify the lambda expression to

features -> new SimpleBooleanProperty(features.getValue() != null)


使用相同的方法


Using the same approach

new Callback<TableColumn<UserDetails, Boolean>, TableCell<UserDetails, Boolean>>() {
    @Override public TableCell<UserDetails, Boolean> call(TableColumn<UserDetails, Boolean> personBooleanTableColumn) {
        return new AddPersonCell(window, tableUser);
    }
}

可以更改为

personBooleanTableColumn -> new AddPersonCell(window, tableUser)

这篇关于回调Lambda表达式+ JavaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆