Swing rendering seems to be broken in Java 8

Here’s a simple Java program that I wrote to test Swing GUI in Java 8 (I use JDK 1.8.0_05-b13):

 

import javax.swing.JFrame;
import javax.swing.JButton;

public class HelloWorld extends JFrame {

public static void main(String[] args) {
JFrame myWindow = new HelloWorld();

// Creating and adding a button the the container
JButton myButton = new JButton (“Click me”);
myWindow.add(myButton);

myWindow.setSize(200,300);
myWindow.setTitle(“Hello World”);
myWindow.setVisible(true);
}
}

The program renders fine on MAC OS:
HelloWorld_java8_MAC

But this is what I get on Windows 7 Ultimate with JDK 1.8.0_05-b13:
HelloWorld_java8_Windows

In JDK 1.7 under Windows it works fine:

HelloWorld_java7_Windows

I’ve asked some people to try the same code with the same version of Java and they don’t see this issue.

I’ve submitted a bug report to Oracle (it’s in review for two weeks), but if you use Java Swing, test your code before migrating to Java 8.

Update.Running this app with the following command-line parameters helped fixing the situation with bad rendering on Windows:
java -Dsun.java2d.opengl=True HelloWorld
java -Dsun.java2d.d3d=false HelloWorld

So the Swing rendering is broken as it should not depend on the external to JVM software.

13 thoughts on “Swing rendering seems to be broken in Java 8

    1. The code in the test runs Swing code on the main thread. Swing doesn’t support that (and never did).

      Please call Swing APIs on the EDT thread (via SwingUtilities.invokeLater() ) and see if this helps.

      1. While this is true, my HelloWorld is a very simple app that doesn’t include heavy event handling and screen updates. But even if using the worker thread was the reason (will give it a try), Java app should work THE SAME on both platforms.

      2. No, if your application violates the threading contract, it shouldn’t work the same.

        Also, you might want to try running with the -Dsun.java2d.d3d=false command line option to rule out a possible D3D graphics dirvers issue. And updating the video driver on Windows might be a good idea, too.

      3. InvokeLater didn’t make a difference. The code below renders fine on MAC and the same bad way on Windows:

        import javax.swing.JFrame;
        import javax.swing.JButton;
        import javax.swing.SwingUtilities;
        
        public class HelloWorldInvokeLater extends JFrame {
        
          public static void main(String[] args) {
        	  
        	  SwingUtilities.invokeLater(new Runnable() {
        		    public void run() {
        		        createAndShowGUI();
        		    }
        		});	  
          }
        
          private static void createAndShowGUI(){
        	  JFrame myWindow = new HelloWorld();
        	
        	  // Creating and adding a button the the container
        		JButton myButton = new JButton ("Click me");
        	  myWindow.add(myButton); 
        	
        	  myWindow.setSize(200,300);
        	  myWindow.setTitle("Hello World");
        	  myWindow.setVisible(true);   
        	  
        	  myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
          
        }
        
  1. Please try to run you program with these options:
    1) java -Dsun.java2d.opengl=True HelloWorld
    2) java -Dsun.java2d.d3d=True HelloWorld
    3) java -Dsun.java2d.d3d=false HelloWorld

    1. Thank you, Sergey. It’s getting interesting.

      1. These two options helped fixing the situation with bad rendering on Windows:
      java -Dsun.java2d.opengl=True HelloWorld
      java -Dsun.java2d.d3d=false HelloWorld

      2. This one didn’t help
      java -Dsun.java2d.d3d=True HelloWorld

      So the rendering is broken. Will update the bug report at Oracle.

      1. Probably you have an Intel video card or optimus? Please try to update to the latest possible driver from the official site or disable optimus.

        1. This is not the point. Java runs inside the VM. Why the user of my HelloWorld should know anything about video card drivers? What about the Java promise “Write once run anywhere?”

  2. Swing uses java2d to paint a components. Java2D can use different approaches on the different platforms: opengl on osx, d3d/gdi/opengl on windows, xrender on linux.

    In your case there is a problem in d3d implementation, which is selected by default in your environment. The problem can be either in java or in system drivers, that’s why I propose to update the drivers first.(and add this information to the bug report)
    Did you file this issue to bug system?

      1. Is the bug finally in the java bug tracking system? If so, could you provide us the appropriate ID? Thanks in Advance!

Leave a comment