5

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.

JeremiahDuane
  • 374
  • 4
  • 15
  • 1
    Are you writing a JavaFX or Swing application? Or are you embedding one in the other? – Slaw Apr 01 '19 at 00: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). – JeremiahDuane Apr 01 '19 at 00:44
  • 2
    For Swing, I believe you'd use a [`javax.swing.Timer`](https://docs.oracle.com/en/java/javase/12/docs/api/java.desktop/javax/swing/Timer.html) for UI related tasks and a [`javax.swing.SwingWorker`](https://docs.oracle.com/en/java/javase/12/docs/api/java.desktop/javax/swing/SwingWorker.html) for background tasks. For JavaFX, you'd use the [animation API](https://openjfx.io/javadoc/12/javafx.graphics/javafx/animation/package-summary.html) for UI related tasks and a [`javafx.concurrent.Worker`](https://openjfx.io/javadoc/12/javafx.graphics/javafx/concurrent/Worker.html) for background tasks. – Slaw Apr 01 '19 at 01:07
  • Related: [JavaFX periodic background task](https://stackoverflow.com/questions/9966136/javafx-periodic-background-task). – Slaw Apr 01 '19 at 01: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 Jan 11 '23 at 18:04

2 Answers2

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.

Per
  • 340
  • 4
  • 9
  • 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 Feb 09 '23 at 08:49
0

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

-Djdk.gtk.version=2