Example usage for javax.swing JRootPane setJMenuBar

List of usage examples for javax.swing JRootPane setJMenuBar

Introduction

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

Prototype

public void setJMenuBar(JMenuBar menu) 

Source Link

Document

Adds or changes the menu bar used in the layered pane.

Usage

From source file:RootExample2.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JRootPane root = f.getRootPane();

    // Create a menu bar
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    bar.add(menu);//from ww w  .  j  av  a2 s  .  c o m
    menu.add("Open");
    menu.add("Close");
    root.setJMenuBar(bar);

    // Add a button to the content pane
    root.getContentPane().add(new JButton("Hello World"));

    // Display the UI
    f.pack();
    f.setVisible(true);
}

From source file:org.eclipse.wb.internal.swing.preferences.laf.LafPreferencePage.java

/**
 * Creates {@link EmbeddedSwingComposite} with some Swing components to show it using different
 * LAFs.//from w  w  w .j a v a 2  s.  c o m
 */
private void createPreviewArea(Group previewGroup) {
    try {
        LookAndFeel currentLookAndFeel = UIManager.getLookAndFeel();
        EmbeddedSwingComposite awtComposite = new EmbeddedSwingComposite(previewGroup, SWT.NONE) {
            @Override
            protected JComponent createSwingComponent() {
                // create the JRootPane
                JRootPane rootPane = new JRootPane();
                {
                    JMenuBar menuBar = new JMenuBar();
                    rootPane.setJMenuBar(menuBar);
                    {
                        JMenu mnFile = new JMenu(Messages.LafPreferencePage_previewFile);
                        menuBar.add(mnFile);
                        {
                            JMenuItem mntmNew = new JMenuItem(Messages.LafPreferencePage_previewNew);
                            mnFile.add(mntmNew);
                        }
                        {
                            JMenuItem mntmExit = new JMenuItem(Messages.LafPreferencePage_previewExit);
                            mnFile.add(mntmExit);
                        }
                    }
                    {
                        JMenu mnView = new JMenu(Messages.LafPreferencePage_previewView);
                        menuBar.add(mnView);
                        {
                            JMenuItem mntmCommon = new JMenuItem(Messages.LafPreferencePage_previewCommon);
                            mnView.add(mntmCommon);
                        }
                    }
                }
                GridBagLayout gridBagLayout = new GridBagLayout();
                gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
                gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0 };
                gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 1.0E-4 };
                gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0E-4 };
                rootPane.getContentPane().setLayout(gridBagLayout);
                {
                    JLabel lblLabel = new JLabel(Messages.LafPreferencePage_previewLabel);
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.insets = new Insets(0, 0, 5, 5);
                    gbc.gridx = 0;
                    gbc.gridy = 0;
                    rootPane.getContentPane().add(lblLabel, gbc);
                }
                {
                    JButton btnPushButton = new JButton(Messages.LafPreferencePage_previewButton);
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.insets = new Insets(0, 0, 5, 0);
                    gbc.gridx = 1;
                    gbc.gridy = 0;
                    rootPane.getContentPane().add(btnPushButton, gbc);
                }
                {
                    JComboBox comboBox = new JComboBox();
                    comboBox.setModel(new DefaultComboBoxModel(new String[] {
                            Messages.LafPreferencePage_previewCombo, "ComboBox Item 1", "ComboBox Item 2" }));
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.insets = new Insets(0, 0, 5, 5);
                    gbc.fill = GridBagConstraints.HORIZONTAL;
                    gbc.gridx = 0;
                    gbc.gridy = 1;
                    rootPane.getContentPane().add(comboBox, gbc);
                }
                {
                    JRadioButton rdbtnRadioButton = new JRadioButton(Messages.LafPreferencePage_previewRadio);
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.insets = new Insets(0, 0, 5, 0);
                    gbc.gridx = 1;
                    gbc.gridy = 1;
                    rootPane.getContentPane().add(rdbtnRadioButton, gbc);
                }
                {
                    JCheckBox chckbxCheckbox = new JCheckBox(Messages.LafPreferencePage_previewCheck);
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.insets = new Insets(0, 0, 0, 5);
                    gbc.gridx = 0;
                    gbc.gridy = 2;
                    rootPane.getContentPane().add(chckbxCheckbox, gbc);
                }
                {
                    JTextField textField = new JTextField();
                    textField.setText(Messages.LafPreferencePage_previewTextField);
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.fill = GridBagConstraints.HORIZONTAL;
                    gbc.gridx = 1;
                    gbc.gridy = 2;
                    rootPane.getContentPane().add(textField, gbc);
                }
                return rootPane;
            }
        };
        awtComposite.populate();
        // restore current laf
        UIManager.put("ClassLoader", currentLookAndFeel.getClass().getClassLoader());
        UIManager.setLookAndFeel(currentLookAndFeel);
    } catch (Throwable e) {
        DesignerPlugin.log(e);
    }
}