javaswingimportawtutilitiesjdk-internal-api

error when import AWTUtilities


I try to use AWTUtilities if these are enabled so I use the ways

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;


public class MenuPrincipal extends Llaves{

    JFrame Frame=new JFrame();

    }; 

    public MenuPrincipal() {

        try {Class.forName("com.sun.awt.AWTUtilities"); } catch (Exception e) {}
        Frame.setSize(600, 500);
        Frame.setResizable(false);
        Frame.setUndecorated(true);
        Frame.setLayout(null);
        Frame.setLocationRelativeTo(null);
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame.getContentPane().setBackground(Color.WHITE);
        try {AWTUtilities.setWindowOpaque(Frame, false); } catch (Exception e) {}
    }

so it shows an error when I try to compile with netBeans

try {AWTUtilities.setWindowOpaque(Frame, false); } catch (Exception e) {}

but if I use this import, the problem is solved

import com.sun.awt.AWTUtilities;

but when I run the jar in a PC without AWTUtilities it throws an error in the import

import com.sun.awt.AWTUtilities;

I can't put a try catch() in the import, so what can I do?


Solution

  • Overlooking the "other" compiler issues with the example code, Netbeans, or more importantly, Java outputs 2 warnings

    C:\Users\Shane Whitehead\Documents\NetBeansProjects\JavaApplication28\src\javaapplication28\MenuPrincipal.java:8: warning: AWTUtilities is internal proprietary API and may be removed in a future release
    import com.sun.awt.AWTUtilities;
    C:\Users\Shane Whitehead\Documents\NetBeansProjects\JavaApplication28\src\javaapplication28\MenuPrincipal.java:30: warning: AWTUtilities is internal proprietary API and may be removed in a future release
                AWTUtilities.setWindowOpaque(Frame, false);
                ^
    2 warnings
    

    These are trying to tell you that you have made use of a private API, an API which may be removed in the future and which is actually no longer needed if you are using Java 7+

    See How to Create Translucent and Shaped Windows for more details

    Updated

    Now, based on your example, you code will compile, but may not run on Java versions of below 6u10. You should consider using a little reflection to overcome the situation where the API (AWTUtilities) does not exist, for example...

    public static void setOpaque(Window window, boolean opaque) {
    
        try {
    
            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            if (awtUtilsClass != null) {
    
                Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
                method.invoke(null, window, opaque);
    
            }
    
        } catch (Exception exp) {
        }
    
    }
    

    You could also use something like...

    public static boolean supportsPerAlphaPixel() {
    
        boolean support = false;
    
        try {
    
            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            support = true;
    
        } catch (Exception exp) {
        }
    
        return support;
    
    }
    

    To determine if support for transparency is available and make decisions about whether your are going to run or not...as an example

    Updated based on feedback

    Exception in thread "main" java.lang.UnsupportedClassVersionError: DoraditosPatt y/main : Unsupported major.minor version 51.0      
        at java.lang.ClassLoader.defineClass1(Native Method) 
        at java.lang.ClassLoader.defineClass(ClassLoader.java:620) 
    

    This means that...

    1. The classes you are trying to include in your project when you compile it have been compiled with a higher version of Java (was compiled with Java 7, but you're trying to compile them into Java 6)
    2. You've compiled you project using Java 7, but are trying to run it under Java 6

    Or some such, for example