Tag Archives: javaFX

JavaFX textarea component error: ArrayIndexOutOfBoundsException

There was a problem with the gadget made before. From time to time, it reported that the array exceeded the boundary, but the specific code could not be located.

Take some time to solve it today.

The effect you want to achieve is like this: multiple threads display messages on the textarea in turn. In order to ensure real-time data update, you need to constantly refresh the textarea component.

The original code is like this

    public void refresh() {
        if(currentServerName == null) {
            //Refuse to refresh
            return;
        }
        setText("");
        appendText(LOG_MAP.getOrDefault(currentServerName, ""));
        setScrollTop(Double.MAX_VALUE);
        
    }

Directly refresh the data in the textarea in an external thread (a thread created by yourself).

After consulting the data, it is found that the container content must be modified in JavaFX’s own thread to ensure that no error will be reported. So wrap it with the platform. The modified code is as follows:

    public void refresh() {
        if(currentServerName == null) {
            //Refuse to refresh
            return;
        }
        setText("");
        appendText(LOG_MAP.getOrDefault(currentServerName, ""));
        setScrollTop(Double.MAX_VALUE);
        Platform.runLater(()->{
            setText("");
            appendText(LOG_MAP.getOrDefault(currentServerName, ""));
            setScrollTop(Double.MAX_VALUE);
        });
    }

[Solved] Java 9 reflection error: java.lang.reflect.InaccessibleObjectException

Java 9 reflection error

Problem solving

Question

when writing JavaFX, perform the HTTP request database login operation, and convert the returned JSON string into a class object. The following code always reports an error.

// json string to class object
JavaType javaType;
ResponseBean<T> data = new ObjectMapper().readValue(json, javaType);

the following errors are reported:

java.lang.reflect.InaccessibleObjectException: Unable to make protected cc.nsurl.bean.ResponseBean() accessible: module cc.nsurl.controllers does not "opens cc.nsurl.bean" to module com.fasterxml.jackson.databind

if you can’t open the class, you can’t reflect.

Solution:

Add the file module-info.java to the JavaFX project. The overall project structure is as follows:

-- src
   -- main
       -- java
          -- module-info.java

Configure in this java file:

module cc.nsurl.controllers {
    // Classes that require reflection opens Self-written classes to third-party libraries
    opens cc.nsurl.controllers to javafx.fxml;
    opens cc.nsurl.bean to com.fasterxml.jackson.databind;
    exports cc.nsurl.controllers;
}

Access restriction: the type ‘application’ is not API (restriction on required LIB)

When using eclipse to build JavaFX with jdk8 for the first time, a compiler error occurred

Access restriction: The type ‘Application’ is not API (restriction on required library ‘E:\JAVA\jdk1.8\jre\lib\ext\ jfxrt.jar ‘)

 

Solution: right click the project name — & gt; build path — & gt; configure build path — & gt; select JRE System Library — & gt; remove — & gt; add library to add system JRE library

 

Jdk11, using JavaFX in eclipse

Eclipse Version: 2018-12 (4.10.0)
JDK Version 11.0.2
Windows 64
First of all, the official documentation for JavaFX,
, is very detailed, and this article does not follow it exactly.
Download an appropriate JavaFX runtime Download an appropriate JavaFX runtime extract to folder (location not fixed but remember path), for example:
D:\openjfx-11.0.2_windows-x64_bin-sdk\javafx-sdk-11.0.2"
2. Ecliplse has been downloaded by default and Java11.0.2 development environment has been configured. Note that since Java11, the JDK and javaFX have been separated (there is no jre package for javaFX in the JDK after exporting the jre folder from the command line), so you don’t have to download a e(fx)clipse plug-in to use, you have to download and add it manually.
3. Create a User Library according to the official document. Include the jre under lib in the JavaFX runtime file downloaded before
Eclipse -> Window -> Preferences -> Java -> Build Path -> User Libraries -> New
next name is arbitrary, you can find the added FX lib in the Library when creating the project
4. Create a Java project, need three files, (someone else’s Github project link), and import a custom Library path.
5. The following problem occurs in the runtime. The solution mentioned in the official document is to add VM parameters
run-& gt; Run Configurations... select Main. Java is the Main class also click the Arguments TAB you can see the Arguments of the VM option, add the following parameters
- the module - the path "/ path/to \ \ lib" deployment headaches - SDK - 11 - add - modules deployment headaches. Controls, deployment headaches. FXML
Here, the path, to 11 \ lib \ deployment headaches - SDK - parameter is the path of the openjfx before download file decompression, such as
- the module - path "D: \ eclipse \ openjfx - 11.0.2 _windows x64_bin - SDK \ deployment headaches - SDK - 11.0.2 \ lib" - add - modules deployment headaches. Controls, deployment headaches. FXML
The successful running
The import Javafx cannot be resolved
JavaFX Runtime Components are missing, and are required to run this application
Problem 3. Error: The Main class Application.main cannot be initialized
As for the first problem, it is probably due to the
problem caused by running javaFX in the way of installing plug-ins when Java11 JDK and javaFX are separated at the beginning. If there is a problem with the import of Library package, check the path or go directly into the path to see if all of these jre files exist.