Maven中的Javafx TableView:不兼容的类型

时间:2016-12-19 09:03:33

标签: java maven netbeans javafx tableview

当我运行以下命令时,我在NetBean 8.2中使用maven javafx(运行JDK 8时默认)来创建表

   @FXML
    private TableColumn<NewClass, String> colSTT;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        colSTT.setCellFactory(TextFieldTableCell.forTableColumn());
    }

和FXML

<TableView xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="imperfect.FXMLController">
  <columns>
    <TableColumn fx:id="colSTT" prefWidth="75.0" text="C1" />
  </columns>
</TableView>

和Pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>imperfect</groupId>
    <artifactId>Imperfect</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>Imperfect</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mainClass>imperfect.MainApp</mainClass>
    </properties>

    <organization>
        <!-- Used as the 'Vendor' for JNLP generation -->
        <name>Your Organisation</name>
    </organization>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeScope>system</excludeScope>
                            <excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
                            <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>

                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>${java.home}/../bin/javafxpackager</executable>
                            <arguments>
                                <argument>-createjar</argument>
                                <argument>-nocss2bin</argument>
                                <argument>-appclass</argument>
                                <argument>${mainClass}</argument>
                                <argument>-srcdir</argument>
                                <argument>${project.build.directory}/classes</argument>
                                <argument>-outdir</argument>
                                <argument>${project.build.directory}</argument>
                                <argument>-outfile</argument>
                                <argument>${project.build.finalName}.jar</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-cli</id>
                        <goals>
                            <goal>exec</goal>                            
                        </goals>
                        <configuration>
                            <executable>${java.home}/bin/java</executable>
                            <commandlineArgs>${runfx.args}</commandlineArgs>
                        </configuration>
                    </execution>
                </executions>  
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
                <configuration>
                    <additionalClasspathElements>
                        <additionalClasspathElement>${java.home}/lib/jfxrt.jar</additionalClasspathElement>
                    </additionalClasspathElements>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.fxml</include>
                    <include>**/*.css</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/test/java</directory>
                <includes>
                    <include>**/*.fxml</include>
                    <include>**/*.css</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.fxml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

总是犯错误如下

COMPILATION ERROR : 
-------------------------------------------------------------
imperfect/FXMLController.java:[28,64] incompatible types: javafx.util.Callback<javafx.scene.control.TableColumn<java.lang.Object,java.lang.String>,javafx.scene.control.TableCell<java.lang.Object,java.lang.String>> cannot be converted to javafx.util.Callback<javafx.scene.control.TableColumn<imperfect.NewClass,java.lang.String>,javafx.scene.control.TableCell<imperfect.NewClass,java.lang.String>>
1 error

如果我在javafx上运行相同的命令(不是maven)成功。请帮我修复它!

1 个答案:

答案 0 :(得分:2)

您的Maven配置设置为将代码编译为Java 7(或1.7)。正如James_D所说,这意味着Java(或者更准确地说,java编译器javac)无法正确推断出你的回调类型。

将配置更改为编译为Java 8(1.8)应该可以解决您的问题:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <compilerArguments>
            <bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
        </compilerArguments>
    </configuration>
</plugin>

另请参阅the documentation了解Maven编译器插件。

如果出于某种原因需要使用Java 7编译的代码(为了向后兼容),您可以使用James_D提出的明确设置类型的解决方案

colSTT.setCellFactory(TextFieldTableCell.<NewClass>forTableC‌​olumn()); 

但除非您确定需要向后兼容Java 7,否则最好用Java 8编译代码。