6

I am writing a javafx program and I need the panel to update at a constant rate. Right now it is set to update every second. But I got this error, which is usually (but not always) followed by a glitch in the panel when the whole scene becomes distorted (it like mirrors in on itself in a weird choppy x pattern. hard to explain).

Full error: (java:22494): Gdk-WARNING **: 18:38:59.118: XSetErrorHandler() called with a GDK error trap pushed. Don't do that.

This is the code I have for the timer:

Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run()  {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String x = txtDisplay.getText();
                            txtDisplay.setText(x.substring(1, x.length()) + x.substring(0, 1));
                        } catch (NullPointerException e) {
                            System.out.println("Error.");
                        }
                    }
                });
            }
        }, 0, 500);

I think the issue is with the above block, like maybe I am breaking some fundamental swing rule. My other idea is that it has something with two methods editing the same text area at the same time, because I have other methods setting the text area's text.

I would be happy with either a solution to the error or a better way of executing the above method. Just needs to run every second without crashing.

Thanks.

EDIT: A new developement, I am now consistently getting a "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" but the stacktrace does not reference any locations that are in my code.

5
  • 1
    Are you writing a JavaFX or Swing application? Or are you embedding one in the other?
    – Slaw
    Commented Apr 1, 2019 at 0:33
  • I am writing a javafx application, but to do the timer I looked it up and they used a swing embed from what I could tell, so the timer is a swing embed (I could be wrong though, I am a beginner of sorts). Commented Apr 1, 2019 at 0:44
  • 2
    For Swing, I believe you'd use a javax.swing.Timer for UI related tasks and a javax.swing.SwingWorker for background tasks. For JavaFX, you'd use the animation API for UI related tasks and a javafx.concurrent.Worker for background tasks.
    – Slaw
    Commented Apr 1, 2019 at 1:07
  • Related: JavaFX periodic background task.
    – Slaw
    Commented Apr 1, 2019 at 1:09
  • For me, I was using a standard JOptionPane for dialog boxes in my JavaFX project that's when I was getting that error, I changed to using JavaFX alert dialog then the error disappeared.
    – Kennerdol
    Commented Jan 11, 2023 at 18:04

2 Answers 2

8

This looks like is is related to JDK-8156779 (see also JDK-8211305). I.e. there are some issues with JDK 8, 9 and 11 and GTK 3.

For me (on Linux Mint 20.1 and Open JDK 11, Bellsoft 11.0.10.fx-librca) the solution was to force GTK 2 with -Djdk.gtk.version=2

Others have solved it by upgrading to JDK 12 or higher.

3
  • 1
    "Others have solved it by upgrading to JDK 12 or higher" is not reliable. I get the error on Bellsoft LIberica JDK 17 during this code: Runtime.getRuntime().exec().
    – Wortig
    Commented Feb 9, 2023 at 8:49
  • I second Wortig's comment. I get this with Linux Mint on Java 17. The last sentence in this answer is just wrong... I still get the error with -Djdk.gtk.version=2 as a VM arg too...
    – Manius
    Commented Jun 3 at 23:40
  • I have the same issue with Open JDK 21.0.3, and openjfx 21.0.2, and it's not working: WARNING: A command line option tried to select the GTK 2 library, which was removed from JavaFX. WARNING: The GTK 3 library will be used instead
    – iriiko
    Commented 2 days ago
0

Under Eclipse go to run/run configuration and add the following option to vm argument under (x)=Arguments tab.

-Djdk.gtk.version=2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.