Error with package com.sun.glass.ui while learning Java Native Access

1

I am trying to use an Undecorated stage in a JavaFX project, stage.initStyle(StageStyle.UNDECORATED);. It is a modular Gradle project. It is also a multi-project build, though because I am working on it in IntelliJ, it might be more appropriate to refer to it as a multi-module build.

I wish to be able to add regular functionality to this Undecorated stage. I have already been able to add the usual minimize, restore, and close buttons, but if a user clicks on the program's icon in the Windows Task Bar, I want it to minimize and restore as well.

enter image description here

I have found code in this older StackOverflow post that might do this, but I have run into an error I can't quite figure out.

brian's (the poster's) Code:

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import static com.sun.jna.platform.win32.WinUser.GWL_STYLE;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JNATest extends Application {
    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage stage) {
        TextArea ta = new TextArea("output\n");
        VBox root = new VBox(5,ta);
        Scene scene = new Scene(root,800,200);
        stage.setTitle("Find this window");
        stage.setScene(scene);
        stage.show();
        //gets this window (stage)
        long lhwnd = com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow();
        Pointer lpVoid = new Pointer(lhwnd);
        //gets the foreground (focused) window
        final User32 user32 = User32.INSTANCE;
        char[] windowText = new char[512];
        HWND hwnd = user32.GetForegroundWindow();
        //see what the title is
        user32.GetWindowText(hwnd, windowText, 512);
        //user32.GetWindowText(new HWND(lpVoid), windowText, 512);//to use the hwnd from stage
        String text=(Native.toString(windowText));
        //see if it's the same pointer
        ta.appendText("HWND java:" + lpVoid + " HWND user32:"+hwnd+" text:"+text+"\n");
        //change the window style if it's the right title
        if (text.equals(stage.getTitle())){
            //the style to change 
            int WS_DLGFRAME = 0x00400000;//s/b long I think
            //not the same constant here??
            ta.appendText("windows api:"+WS_DLGFRAME+" JNA: "+WinUser.SM_CXDLGFRAME);
            int oldStyle = user32.GetWindowLong(hwnd, GWL_STYLE);
            int newStyle = oldStyle & ~0x00400000; //bitwise not WS_DLGFRAME means remove the style
            newStyle = newStyle & ~0x00040000;//WS_THICKFRAME   
            user32.SetWindowLong(hwnd, GWL_STYLE, newStyle);
        }
    }

}

The part that causes me the error is the com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow();

The Error:

package com.sun.glass.ui is not visible
(package com.sun.glass.ui is declared in module javafx.graphics, which does not export it to module apple.orange.main)

I don't really understand this error well. Is the sun.glass.ui package declared in javafx.graphics because I tried to use it in my code or does it reside inside the javafx.graphics module but is not able to be accessed? My best guess is that this package belongs to some module I don't know the name of which I need to include as a dependency in Gradle and as a requires in my module-info.java file. This is something I am capable of doing, but my Google searches have turned up very little which explains where this package comes from. The comments under this SO question suggest that such com.sun packages should not be used at all and seem to imply that there is probably some equivalent better used.

java
javafx
jna
module-info
asked on Stack Overflow Nov 29, 2019 by Jack J • edited Nov 30, 2019 by Jack J

1 Answer

1

I encountered this same issue in Windows 10. Thankfully, @Slaw 's suggestion was correct.

You need to go to VM options in your IDE (Run -> Edit Configurations..., in IntelliJ) and add this:

--add-exports
javafx.graphics/com.sun.glass.ui=ALL-UNNAMED

It should work after that.

answered on Stack Overflow Jan 15, 2020 by Zack

User contributions licensed under CC BY-SA 3.0