Example usage for javax.swing LookAndFeel LookAndFeel

List of usage examples for javax.swing LookAndFeel LookAndFeel

Introduction

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

Prototype

LookAndFeel

Source Link

Usage

From source file:GUI.MainWindow.java

/**
 * @param args the command line arguments
 *//*from   w  w  w . j av  a  2s . c o m*/
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {

        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    }
    //</editor-fold>

    // Create right click context menu for most Text Components
    final JPopupMenu copypaste = new JPopupMenu();

    JMenuItem cut = new JMenuItem(new DefaultEditorKit.CutAction());
    cut.setText("Cut");
    copypaste.add(cut);

    JMenuItem copy = new JMenuItem(new DefaultEditorKit.CopyAction());
    copy.setText("Copy");
    copypaste.add(copy);

    JMenuItem paste = new JMenuItem(new DefaultEditorKit.PasteAction());
    paste.setText("Paste");
    copypaste.add(paste);

    // Taken from here. It succinctly added a right click context menu to every text component.
    // http://stackoverflow.com/questions/19424574/adding-a-context-menu-to-all-swing-text-components-in-application
    javax.swing.UIManager.addAuxiliaryLookAndFeel(new LookAndFeel() {
        private final UIDefaults defaults = new UIDefaults() {
            public javax.swing.plaf.ComponentUI getUI(JComponent c) {
                if (c instanceof javax.swing.text.JTextComponent) {
                    if (c.getClientProperty(this) == null) {
                        c.setComponentPopupMenu(copypaste);
                        c.putClientProperty(this, Boolean.TRUE);
                    }
                }
                return null;
            }
        };

        @Override
        public UIDefaults getDefaults() {
            return defaults;
        }

        ;

        @Override
        public String getID() {
            return "TextContextMenu";
        }

        @Override
        public String getName() {
            return getID();
        }

        @Override
        public String getDescription() {
            return getID();
        }

        @Override
        public boolean isNativeLookAndFeel() {
            return false;
        }

        @Override
        public boolean isSupportedLookAndFeel() {
            return true;
        }
    });

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MainWindow().setVisible(true);
        }
    });
}