带有java sdk 12的JFXTextField的IllegalAccessException
作者:互联网
我在我的javafx应用程序中添加了JFXTextField,但是我得到了这个错误,并不知道如何解决它
class com.jfoenix.skins.JFXTextFieldSkin (in module com.jfoenix) cannot access a member of class javafx.scene.control.skin.TextFieldSkin (in module javafx.controls) with modifiers “private”
控制器:
package sample;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private Button clickMe;
@FXML
private JFXButton materialButton;
@FXML
private JFXTextField textField;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
materialButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent)
{
String text = textField.getText().trim();
System.out.println(text);
}
});
}
}
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #fcda;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Button fx:id="clickMe" layoutX="198.0" layoutY="188.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="225.0" style="-fx-background-color: #fffe;" text="Click Me" textFill="#722929" />
<JFXButton fx:id="materialButton" layoutX="231.0" layoutY="82.0" prefHeight="54.0" prefWidth="139.0" style="-fx-background-color: #ffff;" textFill="#280c0c">
<font>
<Font size="25.0" />
</font></JFXButton>
<JFXTextField fx:id="textField" layoutX="233.0" layoutY="24.0" promptText="Enter UserName" />
</children>
</AnchorPane>
解决方法:
JFoenix的问题跟踪器已经报道了这个问题:
> JDK 12 incompatible
> IllegalAccessException for JFXTextField / JFXPasswordField with JDK12
对于初学者来说,JFoenix还没有为Java 11做好准备.发布的版本适用于Java 9,但它仍适用于Java 11和JavaFX 11,允许您添加JavaFX依赖项.
但是,在JDK 12下它无法运行,问题与JavaFX无关:即使使用JavaFX 11.0.2,它仍然会失败.
该问题与使用reflection访问TextFieldSkin的Text节点有关:
textNode = ReflectionHelper.getFieldContent(TextFieldSkin.class, this, "textNode");
java.lang.IllegalAccessException: class com.jfoenix.adapters.ReflectionHelper (in module com.jfoenix) cannot access a member of class javafx.scene.control.skin.TextFieldSkin (in module javafx.controls) with modifiers "private"
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:355)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:639)
at java.base/java.lang.reflect.Field.checkAccess(Field.java:1075)
at java.base/java.lang.reflect.Field.get(Field.java:416)
at com.jfoenix/com.jfoenix.adapters.ReflectionHelper.getFieldContent(ReflectionHelper.java:98)
at com.jfoenix/com.jfoenix.skins.JFXTextFieldSkin.<init>(JFXTextFieldSkin.java:59)
虽然这在Java 11.0.2之前工作正常,但Java 12 regression最近的不安全更改阻止了它的工作,并导致textNode = null.
正如@AlanBateman在下面的评论中提到:
[The JFoenix maintainers] should replace their
setAccessible
method to callobj.setAccessible(true)
so that the user gets the right exceptions when the library tries to hack internals that are not accessible to it. If you do that then the user can workaround those issues with--add-exports
or--add-opens
options until the maintainers of the library fix their issues.
目前这意味着坚持使用JDK 11.
或者,您可以尝试构建自己的JFoenix版本,克隆repo(分支9.0.0)并进行必要的更改以使其与JavaFX 11一起使用(超出此答案的范围…),并删除使用尽可能反思.
例如,可以使用以下命令直接检索textNode:
textNode = textPane.getChildren().get(1);
或者,仍然依赖于反思,但提到了适当的变化:
try {
Field field = cls.getDeclaredField(fieldName);
field.setAccessible(true); // <-- Use this.
return (T) field.get(obj);
} catch (Throwable ex) { }
结合:
--add-exports=javafx.controls/javafx.scene.control.skin=$moduleName
标签:java,javafx,jfoenix 来源: https://codeday.me/bug/20190823/1696895.html