Example usage for javax.swing UIManager getLookAndFeel

List of usage examples for javax.swing UIManager getLookAndFeel

Introduction

In this page you can find the example usage for javax.swing UIManager getLookAndFeel.

Prototype

public static LookAndFeel getLookAndFeel() 

Source Link

Document

Returns the current look and feel or null.

Usage

From source file:Main.java

/**
 * Fixes button opacity issues./*from  w  w  w .  j av  a  2  s.c  o m*/
 * This is particularly a problem with JButton on Macintosh;
 * when the background color of its container is changed,
 * a button will appear to be sitting on a gray rectangle.
 *
 * @param button
 */
public static void fixButtonOpacity(JButton button) {
    // If the LAF isn't Metal or a derivative, setOpaque( false ) for the button
    if (!(UIManager.getLookAndFeel() instanceof MetalLookAndFeel)) {
        button.setOpaque(false);
    }
}

From source file:Main.java

/**
 * Creates a sub-menu for changing Look'n'Feel.
 * @param rootComponent the root component which Look'n'Feel should be changed (child component are processed recursively).
 * @return a menu item with sub-menu items for each Look'n'Feel
 * installed in the system with associated actions to change the
 * Look'n'Feel for the <code>rootComponent</root>.
 *//* w w  w  .j a v a 2 s  . c  om*/
public static JMenu getLafMenu(final Component rootComponent) {
    JMenu jMenu = new JMenu("Look & Feel");
    ButtonGroup buttonGroup = new ButtonGroup();
    final UIManager.LookAndFeelInfo[] installedLFs = UIManager.getInstalledLookAndFeels();
    String currentLF = UIManager.getLookAndFeel().getName();
    for (int i = 0; i < installedLFs.length; i++) {
        JCheckBoxMenuItem jMenuItem = new JCheckBoxMenuItem(installedLFs[i].getName());
        jMenu.add(jMenuItem);
        buttonGroup.add(jMenuItem);
        jMenuItem.setState(currentLF.equals(installedLFs[i].getName()));
        class ChangeLF extends AbstractAction {
            private UIManager.LookAndFeelInfo iLF;

            public ChangeLF(UIManager.LookAndFeelInfo iLF) {
                super(iLF.getName());
                this.iLF = iLF;
            }

            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager.setLookAndFeel(iLF.getClassName());
                    SwingUtilities.updateComponentTreeUI(rootComponent);
                } catch (Exception ex) {
                    System.out.print("Could not set look and feel: " + ex.toString());
                }
            }
        }
        jMenuItem.setAction(new ChangeLF(installedLFs[i]));
    }
    return jMenu;
}

From source file:Main.java

/**
 * Swing menus are looking pretty bad on Linux when the GTK LaF is used (See
 * bug #6925412). It will most likely never be fixed anytime soon so this
 * method provides a workaround for it. It uses reflection to change the GTK
 * style objects of Swing so popup menu borders have a minimum thickness of
 * 1 and menu separators have a minimum vertical thickness of 1.
 *///w ww .j  av a 2 s  .  c  om
public static void installGtkPopupBugWorkaround() {
    // Get current look-and-feel implementation class
    LookAndFeel laf = UIManager.getLookAndFeel();
    Class<?> lafClass = laf.getClass();

    // Do nothing when not using the problematic LaF
    if (!lafClass.getName().equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"))
        return;

    // We do reflection from here on. Failure is silently ignored. The
    // workaround is simply not installed when something goes wrong here
    try {
        // Access the GTK style factory
        Field field = lafClass.getDeclaredField("styleFactory");
        boolean accessible = field.isAccessible();
        field.setAccessible(true);
        Object styleFactory = field.get(laf);
        field.setAccessible(accessible);

        // Fix the horizontal and vertical thickness of popup menu style
        Object style = getGtkStyle(styleFactory, new JPopupMenu(), "POPUP_MENU");
        fixGtkThickness(style, "yThickness");
        fixGtkThickness(style, "xThickness");

        // Fix the vertical thickness of the popup menu separator style
        style = getGtkStyle(styleFactory, new JSeparator(), "POPUP_MENU_SEPARATOR");
        fixGtkThickness(style, "yThickness");
    } catch (Exception e) {
        // Silently ignored. Workaround can't be applied.
    }
}

From source file:Main.java

/**
 * gives default modifier of the current OS.
 * //  ww w .j av a  2s  . com
 * @return meta (command) for OSX, control for Windows/Linux etc 
 */
public static int getSystemDefaultModifier() {
    if (!(UIManager.getLookAndFeel().getID().equals(METAL_LAF_ID))) {
        int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
        if (mask == Event.META_MASK) {
            return KeyEvent.VK_META;
        } else if (mask == Event.ALT_MASK) {
            return KeyEvent.VK_ALT;
        }
    }
    return KeyEvent.VK_CONTROL;
}

From source file:Main.java

/**
 * Initialises the {@link JDialog} for the {@link JComponent}.
 * /*w  ww.  ja  v  a 2  s .c om*/
 * @param dialog
 * @param component
 * @param parentComponent
 */
private static void initDialog(final JDialog dialog, final JComponent component,
        final Component parentComponent) {
    dialog.setResizable(true);
    dialog.setComponentOrientation(component.getComponentOrientation());
    Container contentPane = dialog.getContentPane();

    contentPane.setLayout(new BorderLayout());
    contentPane.add(component, BorderLayout.CENTER);

    final int buttonWidth = 75;

    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(2, 4, 4, 4));

    buttonPanel.add(Box.createHorizontalGlue());

    @SuppressWarnings("serial")
    final Action closeAction = new AbstractAction("Close") {
        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    };

    final JButton button = new JButton(closeAction);
    fixWidth(button, buttonWidth);
    buttonPanel.add(button);

    contentPane.add(buttonPanel, BorderLayout.SOUTH);

    if (JDialog.isDefaultLookAndFeelDecorated()) {
        boolean supportsWindowDecorations = UIManager.getLookAndFeel().getSupportsWindowDecorations();
        if (supportsWindowDecorations) {
            dialog.setUndecorated(true);
            component.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        }
    }
    dialog.pack();
    dialog.setLocationRelativeTo(parentComponent);
    WindowAdapter adapter = new WindowAdapter() {
        //         private boolean gotFocus = false;
        public void windowClosing(WindowEvent we) {
            fireAction(we.getSource(), closeAction, "close");
        }
    };
    dialog.addWindowListener(adapter);
    dialog.addWindowFocusListener(adapter);
}

From source file:Main.java

public static void initComponentHeight(final Component... components) {
    if (components == null) {
        return;//from  w  w w.  ja va  2 s  . c om
    }
    for (final Component component : components) {
        if ((component instanceof JComboBox) || (component instanceof JButton)) {
            component.setPreferredSize(new Dimension(component.getPreferredSize().width, 22));
        } else if (component instanceof JTextField) {
            final String lf = UIManager.getLookAndFeel().getClass().getName();
            int i = 22;
            if (lf.equals(LAF_METAL)) {
                i = 23;
            }
            component.setPreferredSize(new Dimension(component.getPreferredSize().width, i));
        }
    }
}

From source file:LookAndFeelPrefs.java

/**
 * Create a menu of radio buttons listing the available Look and Feels. When
 * the user selects one, change the component hierarchy under frame to the new
 * LAF, and store the new selection as the current preference for the package
 * containing class c./*from  w  ww. j a v a2 s . c  o  m*/
 */
public static JMenu createLookAndFeelMenu(final Class prefsClass, final ActionListener listener) {
    // Create the menu
    final JMenu plafmenu = new JMenu("Look and Feel");

    // Create an object used for radio button mutual exclusion
    ButtonGroup radiogroup = new ButtonGroup();

    // Look up the available look and feels
    UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();

    // Find out which one is currently used
    String currentLAFName = UIManager.getLookAndFeel().getClass().getName();

    // Loop through the plafs, and add a menu item for each one
    for (int i = 0; i < plafs.length; i++) {
        String plafName = plafs[i].getName();
        final String plafClassName = plafs[i].getClassName();

        // Create the menu item
        final JMenuItem item = plafmenu.add(new JRadioButtonMenuItem(plafName));
        item.setSelected(plafClassName.equals(currentLAFName));

        // Tell the menu item what to do when it is selected
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                // Set the new look and feel
                try {
                    UIManager.setLookAndFeel(plafClassName);
                } catch (UnsupportedLookAndFeelException e) {
                    // Sometimes a Look-and-Feel is installed but not
                    // supported, as in the Windows LaF on Linux platforms.
                    JOptionPane.showMessageDialog(plafmenu,
                            "The selected Look-and-Feel is " + "not supported on this platform.",
                            "Unsupported Look And Feel", JOptionPane.ERROR_MESSAGE);
                    item.setEnabled(false);
                } catch (Exception e) { // ClassNotFound or Instantiation
                    item.setEnabled(false); // shouldn't happen
                }

                // Make the selection persistent by storing it in prefs.
                Preferences p = Preferences.userNodeForPackage(prefsClass);
                p.put(PREF_NAME, plafClassName);

                // Invoke the supplied action listener so the calling
                // application can update its components to the new LAF
                // Reuse the event that was passed here.
                listener.actionPerformed(event);
            }
        });

        // Only allow one menu item to be selected at once
        radiogroup.add(item);
    }

    return plafmenu;
}

From source file:JToolbarToggleButton.java

/**
 * Initializes the button.// ww  w.j a  va  2  s.c  om
 */
protected void initialize() {
    if (!System.getProperty("java.version").startsWith("1.3")) {
        setOpaque(false);
        setBackground(new java.awt.Color(0, 0, 0, 0));
    }
    setBorderPainted(false);
    setMargin(new Insets(2, 2, 2, 2));

    // Windows XP look and feel seems to have a bug due to which the
    // size of the parent container changes when the border painted
    // property is set. Temporary fix: disable mouseover behavior if
    // installed lnf is Windows XP
    if (!UIManager.getLookAndFeel().getName().equals("Windows")) {
        addMouseListener(new MouseListener());
    }
}

From source file:com.igormaznitsa.jhexed.swing.editor.ui.Utils.java

public static boolean isUnderNimbusLookAndFeel() {
    return UIManager.getLookAndFeel().getName().contains("Nimbus");
}

From source file:com.igormaznitsa.jhexed.swing.editor.ui.Utils.java

public static boolean isUnderWindowsClassicLookAndFeel() {
    return UIManager.getLookAndFeel().getName().equals("Windows Classic");
}