Java Window setWindowAlpha(Window w, int value)

Here you can find the source of setWindowAlpha(Window w, int value)

Description

set Window Alpha

License

Open Source License

Declaration

static final void setWindowAlpha(Window w, int value) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class Main {
    static final void setWindowAlpha(Window w, int value) {
        if (w == null)
            return;

        if (w.getPeer() == null) {
            w.pack();/*from  w  w w  .jav  a2  s  .co  m*/
        }
        java.awt.peer.ComponentPeer peer = w.getPeer();

        try {
            // Alpha API for Apple's Java 1.4 + 1.5 on Mac OS X 10.4 Tiger.
            invoke(peer, "setAlpha", (float) (value / 255f));
        } catch (NoSuchMethodException e) {
            // Alpha API for Apple's Java 1.3.
            try {
                invoke(peer, "_setAlpha", value);
            } catch (NoSuchMethodException e2) {
                // Platform neutral API
                w.setBackground(new Color(255, 255, 255, value));
                if (w instanceof RootPaneContainer) {
                    ((RootPaneContainer) w).getContentPane().setBackground(new Color(255, 255, 255, 0));
                }
            }
        }
    }

    public static Object invoke(Object obj, String methodName, boolean newValue) throws NoSuchMethodException {
        try {
            Method method = obj.getClass().getMethod(methodName, new Class[] { Boolean.TYPE });
            return method.invoke(obj, new Object[] { new Boolean(newValue) });
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName + " is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }

    public static Object invoke(Object obj, String methodName, int newValue) throws NoSuchMethodException {
        try {
            Method method = obj.getClass().getMethod(methodName, new Class[] { Integer.TYPE });
            return method.invoke(obj, new Object[] { new Integer(newValue) });
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName + " is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }

    public static Object invoke(Object obj, String methodName, float newValue) throws NoSuchMethodException {
        try {
            Method method = obj.getClass().getMethod(methodName, new Class[] { Float.TYPE });
            return method.invoke(obj, new Object[] { new Float(newValue) });
        } catch (IllegalAccessException e) {
            throw new NoSuchMethodException(methodName + " is not accessible");
        } catch (InvocationTargetException e) {
            // The method is not supposed to throw exceptions
            throw new InternalError(e.getMessage());
        }
    }
}

Related

  1. modifyDimensions(Window window)
  2. openWindow(final JPanel panel)
  3. putWindowNormalBounds(Window window, Rectangle bounds)
  4. removeWindow(Window w)
  5. repaintMnemonicsInWindow(Window w)
  6. setWindowOpacity(Window window, float opacity)
  7. show(final Container container, final boolean visible, Window appearOnLeftSide)
  8. show(JWindow window)
  9. showLater(final Window win)