Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * 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/>.
 */

import java.awt.AWTEvent;
import java.awt.Container;
import java.awt.event.KeyAdapter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.swing.JComponent;

public class Main {
    private static final long MOUSE_MASK = AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK
            | AWTEvent.MOUSE_WHEEL_EVENT_MASK;
    private static Field dispatcherField;
    private static Constructor<?> newLightweightDispatcher;
    private static Method dispatchMethod;
    private static Method enableEvents;

    static void setLightweightDispatcher(JComponent component) {
        if (dispatcherField == null) {
            initReflection();
        }
        try {
            Object dispatcher = newLightweightDispatcher.newInstance(component);
            enableEvents.invoke(dispatcher, MOUSE_MASK | AWTEvent.KEY_EVENT_MASK);
            dispatcherField.set(component, dispatcher);
            component.addKeyListener(new KeyAdapter() {
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    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;
        }
    }
}