Java Swing UIManager fixGtkPopupStyle()

Here you can find the source of fixGtkPopupStyle()

Description

fix Gtk Popup Style

License

Apache License

Declaration

private static void fixGtkPopupStyle() 

Method Source Code


//package com.java2s;
/*//w w  w .  j ava  2  s  . c o  m
 * Copyright 2012 Miklos Juhasz (mjuhasz), JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.*;
import javax.swing.plaf.synth.Region;
import javax.swing.plaf.synth.SynthLookAndFeel;
import javax.swing.plaf.synth.SynthStyle;
import javax.swing.plaf.synth.SynthStyleFactory;

import java.lang.reflect.Field;

public class Main {
    private static void fixGtkPopupStyle() {
        if (!isUnderGTKLookAndFeel()) {
            return;
        }
        final SynthStyleFactory original = SynthLookAndFeel.getStyleFactory();
        SynthLookAndFeel.setStyleFactory(new SynthStyleFactory() {
            @Override
            public SynthStyle getStyle(final JComponent c, final Region id) {
                final SynthStyle style = original.getStyle(c, id);
                if (id == Region.POPUP_MENU) {
                    fixPopupMenuStyle(style);
                } else if (id == Region.POPUP_MENU_SEPARATOR) {
                    fixPopupMenuSeparatorStyle(style);
                }
                return style;
            }

            private void fixPopupMenuStyle(SynthStyle style) {
                try {
                    Field f = getAccessibleFieldFromStyle(style, "xThickness");
                    final Object x = f.get(style);
                    if (x instanceof Integer && (Integer) x == 0) {
                        f.set(style, 1);
                        f = getAccessibleFieldFromStyle(style, "yThickness");
                        f.set(style, 3);
                    }
                } catch (Exception ignore) {
                    // ignore
                }
            }

            private void fixPopupMenuSeparatorStyle(SynthStyle style) {
                try {
                    Field f = getAccessibleFieldFromStyle(style, "yThickness");
                    final Object y = f.get(style);
                    if (y instanceof Integer && (Integer) y == 0) {
                        f.set(style, 2);
                    }
                } catch (Exception ignore) {
                    // ignore
                }
            }

            private Field getAccessibleFieldFromStyle(SynthStyle style, String fieldName)
                    throws NoSuchFieldException {
                Field f = style.getClass().getDeclaredField(fieldName);
                f.setAccessible(true);
                return f;
            }
        });

        new JPopupMenu(); // invokes updateUI() -> updateStyle()
        new JPopupMenu.Separator(); // invokes updateUI() -> updateStyle()

        SynthLookAndFeel.setStyleFactory(original);
    }

    public static boolean isUnderGTKLookAndFeel() {
        return UIManager.getLookAndFeel().getName().contains("GTK");
    }
}

Related

  1. defaultColumnWidth()
  2. destylize(T comp)
  3. disabledIcon(JComponent parent, Icon icon)
  4. disablePaintSliderValue()
  5. drawVistaBackground(Graphics g, Component b, String prefix)
  6. fixHtmlDisplay(JComponent component)
  7. getAnimationSpeed()
  8. getClassLoaderForResources()
  9. getClassLoaderForResources()