javalinuxswinggtkjprogressbar

JProgressBar slow on Linux


I am working on a project where I have progress bars to indicate the status of a game controller axis (where events occur on a separate thread). The event callback works, however when I try to display the current value (from events) with a progress bar on Linux it does not work well. On windows the motion is smooth, but on linux it seems to stutter as the progress bar's value changes. I put together a minimal example showing this (without the need to a native library to handle gamepads).

import java.awt.BorderLayout;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class Test extends JFrame {

    JProgressBar bar;

    public Test() {

        bar = new JProgressBar(0, 100);

        setLayout(new BorderLayout());
        add(bar, BorderLayout.NORTH);

        setSize(500, 300);
        setLocationRelativeTo(null);

        EventSimulateThread t = new EventSimulateThread();
        t.start();

    }

    public void updateProgress(int value) {
        bar.setValue(value);
    }

    public static void main(String args[]) {
        Test t = new Test();
        t.setVisible(true);
    }

    class EventSimulateThread extends Thread {
        Random rand = new Random();
        @Override 
        public void run() {
            while(true) {
                for (int i = 0; i <= 100; i++) {
                    final int v = i;
                    SwingUtilities.invokeLater(() -> {
                        updateProgress(v);
                    });
                    try {Thread.sleep(10);}catch(Exception e) {}
                }
                for (int i = 100; i >= 0; i--) {
                    final int v = i;
                    SwingUtilities.invokeLater(() -> {
                        updateProgress(v);
                    });
                    try {Thread.sleep(10);}catch(Exception e) {}
                }
            }
        }
    }
}

When run on windows the progress bar smoothly goes through all values. On linux it jumps around. Any ideas what could cause this?

Edit: I tested this on Ubuntu 18.04 using Gnome3 desktop. In all tests (Windows and Linux) I was using Java 11.


Solution

  • Found the answer here

    Looks like this is due to opengl being disabled by default on Linux. Can be fixed by adding the following line to main.

    System.setProperty("sun.java2d.opengl", "true");