Sets the look of the given dialog. - Java Swing

Java examples for Swing:JDialog

Description

Sets the look of the given dialog.

Demo Code


//package com.java2s;
import javax.swing.*;

import java.awt.*;

import java.lang.reflect.Method;

public class Main {
    public final static int POPUP = 0;
    public final static int UTILITY = 1;
    public final static int NORMAL = 2;

    /**/*from w w w  . ja va 2 s  .  c om*/
     * Sets the look of the given dialog. Works only on Java 7+, but does not
     * crash on Java 6-.
     *
     * @param dialog
     * @param type one of UIUtil.(POPUP, UTILITY, NORMAL)
     */
    public static void decorate(JDialog dialog, int type) {
        try {
            Class windowType = Class.forName("java.awt.Window$Type");
            Method setType = Window.class.getDeclaredMethod("setType",
                    windowType);
            switch (type) {
            case POPUP:
                setType.invoke(dialog, Enum.valueOf(
                        (Class<Enum>) windowType.getDeclaredField("POPUP")
                                .getType(), "POPUP"));
                break;
            case UTILITY:
                setType.invoke(dialog, Enum.valueOf(
                        (Class<Enum>) windowType
                                .getDeclaredField("UTILITY").getType(),
                        "UTILITY"));
                break;
            case NORMAL:
                setType.invoke(dialog, Enum.valueOf(
                        (Class<Enum>) windowType.getDeclaredField("NORMAL")
                                .getType(), "NORMAL"));
                break;
            }

        } catch (Exception e) {
            // This ClassNotFound occurs if the user doesn't have Java 7 installed.
            // We can still create a utility-looking window, though.
            // Just doesn't look as nice.
            dialog.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
        }
    }
}

Related Tutorials