Closed Ali-RS closed 2 years ago
Using AWT/Swing together with GLFW is not really supported/possible, because both try to steal each other's OS/window message events.
AWT is using its own window message loop to process the OS/window message events in its EDT thread and you are doing the same with glfwPollEvents
/glfwWaitEvents
.
@httpdigest thanks for the response.
Afaik LWJGL recently added a modified version of GLFW (glfw_async
) that makes it possible to use AWT/Swing together with GLFW on Mac. Should a similar solution work for Linux too?
glfw_async
currently only targets macOS.
And it also does not allow you to create any AWT/Swing windows.
It only lets you use the ImageIO subsystem (e.g. for image loading/storing) and lets you open/use modal dialog windows.
glfw_async
is basically there to avoid people who already use GLFW to add the -XstartOnFirstThread
JVM argument.
Hi! Another person trying to use AWT/Swing with LWJGL v3. I've been trying to figure out various code examples that cause an application to crash when running on Linux (testing on Ubuntu 20.04) with Java 17 (using Azul Zulu OpenJDK 17.0.2). Here's a problematic code snippet:
package example;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.lwjgl.glfw.GLFW;
public class CodeSnippet
{
public static void main(String[] args)
{
new JFrame();
GLFW.glfwInit();
new JFileChooser().showOpenDialog(null);
}
}
When closing the dialog to open a file, the whole app terminates immediately with the following error message:
X Error of failed request: BadWindow (invalid Window parameter)
Major opcode of failed request: 20 (X_GetProperty)
Resource id in failed request: 0x4a00020
Serial number of failed request: 1515
Current serial number in output stream: 1515
It appears that one of the requests for destroying a window from AWT is resulting in that error.
Some things I've observed from that example:
JFileChooser
with a simple JDialog
or JFrame
and I couldn't get the error. It seems that there is something peculiar about JFileChooser
that I've yet to identify.I've been digging for solutions to get AWT/Swing to work with LWJGL v3, as migrating away from any of the two would result in a massive undertaking and does not seem like a viable solution at the moment.
Making progress on the problematic example and found out that the drag and drop feature from Swing seems to be causing some trouble.
package example;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.TransferHandler;
import javax.swing.WindowConstants;
import org.lwjgl.glfw.GLFW;
public class CodeSnippet
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
GLFW.glfwInit();
TransferHandler newHandler = new TransferHandler("Bloppy");
frame.setTransferHandler(newHandler); // Comment out to get the application to terminate properly.
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(600, 600));
frame.pack();
frame.setVisible(true);
}
}
As soon as a TransferHandler
is added to the frame, the X Error of failed request: BadWindow
will pop up and make the application crash immediately. No idea if that's useful observation but I'll keep digging unless someone has some further knowledge on the more general issue.
That is platform dependent. You need set glfwGetX11Display
and glfwGetX11Window
with AWT/Swing and error will be gone.
For Windows glfwGetWin32Window
and for macOS glfwCocoaWindow
. Read more glfw3native!
Why do you not expect glfw with platform binding like SDL2 has also platform dependence Via SDL_GetWindowWMInfo
?
Thanks @DeafMan1983
You need set glfwGetX11Display and glfwGetX11Window with AWT/Swing and error will be gone.
Not sure how to set these values, can you please show me the code snippet I need to add in my Java code? Thanks!
Hey @Ali-RS & @SylvainBertrand,
Could you please try again with the latest LWJGL snapshot? I cannot reproduce such a crash. I also tried both code snippets by @SylvainBertrand and they worked without issues.
Hi @Spasi
Thanks for your response. I can confirm using the latest snapshot the error does not occur.
Sorry for late delay! Find in glfw native / platform check lwjgl3 packages
You should read glfw3 documentation for platform / native
Version
3.3.0 (nightly)
Platform
Linux x64
JDK
AdoptOpenJDK 16
Module
OpenGL, GLFW
Bug description
When I open a swing window along with the GLFW window, the moment I close the swing window it crashes the app with the below error. I am using LWJGL 3 with jMonkeyEngine.
I am on Linux Mint with Mesa (v20) graphics driver.
Stacktrace or crash log output