Performs lightweight dispatching for the specified event on this swing window. - Java Swing

Java examples for Swing:JComponent

Description

Performs lightweight dispatching for the specified event on this swing window.

Demo Code

/*//from www . j a  v a 2  s . co m
 * This file is part of ThingsFX.
 *
 * ThingsFX is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ThingsFX is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ThingsFX. If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;
import java.awt.AWTEvent;
import java.awt.Container;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.swing.JComponent;

public class Main {
    private static Field dispatcherField;
    private static Constructor<?> newLightweightDispatcher;
    private static Method dispatchMethod;
    private static Method enableEvents;

    /**
     * Performs lightweight dispatching for the specified event on this window.
     * This only calls the lightweight dispatcher. We cannot simply
     * call dispatchEvent() because that would also send the event to the
     * Toolkit dispatching mechanism (AWTEventListener, etc), which has ugly
     * side effects, like popups closing too early.
     *
     * @param e the event to be dispatched
     */
    static void dispatchEvent(AWTEvent awtEvent, JComponent component) {

        if (dispatcherField == null) {
            initReflection();
        }
        try {
            Object dispatcher = dispatcherField.get(component);
            if (dispatcher != null) {
                dispatchMethod.invoke(dispatcher, awtEvent);
            }
        } catch (Exception ex) {
            InternalError err = new InternalError();
            err.initCause(ex);
            throw err;
        }

    }

    static void initReflection() {
        try {

            // lightweight dispatcher
            dispatcherField = Container.class
                    .getDeclaredField("dispatcher");
            dispatcherField.setAccessible(true);

            Class<?> dispatcherCls = Class
                    .forName("java.awt.LightweightDispatcher");
            newLightweightDispatcher = dispatcherCls
                    .getDeclaredConstructor(new Class[] { Container.class });
            newLightweightDispatcher.setAccessible(true);

            dispatchMethod = dispatcherCls.getDeclaredMethod(
                    "dispatchEvent", AWTEvent.class);
            dispatchMethod.setAccessible(true);

            enableEvents = dispatcherCls.getDeclaredMethod("enableEvents",
                    new Class[] { long.class });
            enableEvents.setAccessible(true);

        } catch (Exception ex) {

            System.err.println(ex);

            InternalError err = new InternalError();
            err.initCause(ex);
            throw err;
        }
    }
}

Related Tutorials