Java JComponent Properties setLightweightDispatcher(JComponent component)

Here you can find the source of setLightweightDispatcher(JComponent component)

Description

set Lightweight Dispatcher

License

Open Source License

Declaration

static void setLightweightDispatcher(JComponent component) 

Method Source Code


//package com.java2s;
/*//  ww w . j a va 2s.  c  o  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/>.
 */

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

Related

  1. resetDisplayComponent(JComponent mainComp, JComponent nonDispComp, JComponent newDispComp)
  2. setComponentEnabled(JComponent component, Boolean isEnabled)
  3. setDirty(JComponent comp, boolean isDirty)
  4. setGlassPane(JComponent comp, Component glass, boolean visible)
  5. setLegendVisible(JComponent chartUI, boolean visible)
  6. showAsOnlyVisibleChild(JComponent container, Component childToBeMadeVisible)
  7. showSplash(JComponent content, int milliseconds)
  8. showSplash(JComponent content, int milliseconds, boolean start_timer)