Java Code Examples for javax.swing.JMenuBar

The following examples show how to use javax.swing.JMenuBar. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: ib-controller   Source File: SwingUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
private static void appendMenuItem(Component menuItem, StringBuilder builder, String indent) {
    if (menuItem instanceof JMenuBar) {
        appendMenuSubElements((MenuElement)menuItem, builder, indent);
    } else if (menuItem instanceof JPopupMenu) {
        appendMenuSubElements((MenuElement)menuItem, builder, indent);
    } else if (menuItem instanceof JMenuItem) {
        builder.append(NEWLINE);
        builder.append(indent);
        builder.append(((JMenuItem)menuItem).getText());
        builder.append(((JMenuItem)menuItem).isEnabled() ? "" : "[Disabled]");
        appendMenuSubElements((JMenuItem)menuItem, builder, "|   " + indent);
    } else if (menuItem instanceof JSeparator) {
        builder.append(NEWLINE);
        builder.append(indent);
        builder.append("--------");
    }
}
 
Example 2
Source Project: jdk8u_jdk   Source File: MenuItemIconTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void createUI() throws Exception {
    SwingUtilities.invokeAndWait(() -> {
        frame = new JFrame();
        frame.setTitle("Test");
        JMenuBar menuBar = new JMenuBar();
        ImageIcon icon = createIcon();
        menuItem = new JMenuItem("Command", icon);
        menuItem.setHorizontalTextPosition(SwingConstants.LEFT);
        menuBar.add(menuItem);
        frame.setJMenuBar(menuBar);
        frame.setPreferredSize(new Dimension(500, 500));
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    });
}
 
Example 3
private static void createAndShowGUI(TestItem testItem) {

        frame = new JFrame();
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar menuBar = new JMenuBar();
        menu = new JMenu("Menu");
        JMenuItem menuItem = testItem.getMenuItem();
        testItem.setProperties(menuItem);
        menu.add(menuItem);
        menuBar.add(menu);

        frame.setJMenuBar(menuBar);
        frame.setVisible(true);
    }
 
Example 4
Source Project: arcusplatform   Source File: Main.java    License: Apache License 2.0 6 votes vote down vote up
public static JFrame launch(Component contents, JMenuBar menuBar) {
   window = new JFrame("Oculus");
   window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   if(menuBar != null) {
      window.setJMenuBar(menuBar);
   }
   window.setLayout(new BorderLayout());
   window.getContentPane().add(createNotificationPanel(), BorderLayout.NORTH);
   window.getContentPane().add(contents, BorderLayout.CENTER);
   window.getContentPane().add(createStatusPanel(), BorderLayout.SOUTH);

   int width = 1400;
   int height = 1000;
   Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
   window.setBounds((int)screen.getWidth()/2 - width/2, (int)screen.getHeight()/2 - height/2, width, height);
   window.setLocationByPlatform(true);

   window.setVisible(true);
   Oculus.setMainWindow(window);
   return window;
}
 
Example 5
Source Project: netbeans   Source File: MenuChecker.java    License: Apache License 2.0 6 votes vote down vote up
/** Get MenuBar and tranfer it to ArrayList.
     * @param menu menu to be tranfered
     * @return tranfered menubar */
    private static List<NbMenu> getMenuBarArrayList(JMenuBar menu) {
        visitMenuBar(menu);

        MenuElement [] elements = menu.getSubElements();

        List<NbMenu> list = new ArrayList<NbMenu>();
        for(int k=0; k < elements.length; k++) {
//            if(elements[k] instanceof JMenuItem) {
//                list.add(new NbMenu((JMenuItem)elements[k], null));
                JMenuBarOperator menuOp = new JMenuBarOperator(menu);
                JMenu item = menuOp.getMenu(k);
                list.add(new NbMenu(item, getMenuArrayList(item)));
//            }
            /*if(elements[k] instanceof JMenuBar) {
                JMenuBarOperator menuOp = new JMenuBarOperator(menu);
                list.add(getMenuArrayList(menuOp.getMenu(0)));
            }
             */
        }
        return list;
    }
 
Example 6
Source Project: IBC   Source File: SwingUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
private static void appendMenuItem(Component menuItem, StringBuilder builder, String indent) {
    if (menuItem instanceof JMenuBar) {
        appendMenuSubElements((MenuElement)menuItem, builder, indent);
    } else if (menuItem instanceof JPopupMenu) {
        appendMenuSubElements((MenuElement)menuItem, builder, indent);
    } else if (menuItem instanceof JMenuItem) {
        builder.append(NEWLINE);
        builder.append(indent);
        builder.append(((JMenuItem)menuItem).getText());
        builder.append(((JMenuItem)menuItem).isEnabled() ? "" : "[Disabled]");
        appendMenuSubElements((JMenuItem)menuItem, builder, "|   " + indent);
    } else if (menuItem instanceof JSeparator) {
        builder.append(NEWLINE);
        builder.append(indent);
        builder.append("--------");
    }
}
 
Example 7
Source Project: FCMFrame   Source File: MainFrame.java    License: Apache License 2.0 6 votes vote down vote up
public JMenuBar createMenus() {
	JMenuBar menuBar = new JMenuBar();
	menuBar.getAccessibleContext().setAccessibleName("");

	JMenu fileMenu = (JMenu) menuBar.add(new JMenu("文件"));
	createMenuItem(fileMenu, "打开文件", "", "", new OpenFileAction());
	fileMenu.addSeparator();
	createMenuItem(fileMenu, "保存", "", "", new SaveFileAction());
	createMenuItem(fileMenu, "另存为...", "", "", new SaveAsFileAction());
	fileMenu.addSeparator();
	createMenuItem(fileMenu, "保存界面为图片...", "", "", new SaveAsPictureAction());
	fileMenu.addSeparator();
	createMenuItem(fileMenu, "打开文件位置", "", "", new FilesLocationAction());
	fileMenu.addSeparator();
	createMenuItem(fileMenu, "退出系统", "", "", new ExitSys());
	
	JMenu preferenceMenu = (JMenu) menuBar.add(new JMenu("选项"));
	JMenuItem mi = createCheckBoxMenuItem(preferenceMenu, "显示网格", "", "", new CoordinateAction());
	mi.setSelected(true);

	return menuBar;
}
 
Example 8
Source Project: jdk8u_jdk   Source File: bug8031573.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public void init() {
    try {

        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        SwingUtilities.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                JMenuBar bar = new JMenuBar();
                JMenu menu = new JMenu("Menu");
                JCheckBoxMenuItem checkBoxMenuItem
                        = new JCheckBoxMenuItem("JCheckBoxMenuItem");
                checkBoxMenuItem.setSelected(true);
                menu.add(checkBoxMenuItem);
                bar.add(menu);
                setJMenuBar(bar);
            }
        });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source Project: jaamsim   Source File: GUIFrame.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the Control Panel's menu bar.
 */
private void initializeMenus() {

	// Set up the individual menus
	this.initializeFileMenu();
	this.initializeEditMenu();
	this.initializeToolsMenu();
	this.initializeWindowMenu();
	this.initializeOptionsMenu();
	this.initializeUnitsMenu();
	this.initializeHelpMenu();

	// Add the individual menu to the main menu
	JMenuBar mainMenuBar = new JMenuBar();
	mainMenuBar.add( fileMenu );
	mainMenuBar.add( editMenu );
	mainMenuBar.add( toolsMenu );
	mainMenuBar.add( viewsMenu );
	mainMenuBar.add( optionMenu );
	mainMenuBar.add( unitsMenu );
	mainMenuBar.add( helpMenu );

	// Add main menu to the window
	setJMenuBar( mainMenuBar );
}
 
Example 10
Source Project: IBC   Source File: SwingUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Traverses a container hierarchy and returns the JMenuItem with
 * the given path from the first JMenuBar that contains it
 *
 * @param container
 *  the Container to search in
 * @param path
 *  the required menu path
 * @return
 *  the JMenuItem at the specified path, if found; otherwise null
 */
static JMenuItem findMenuItemInAnyMenuBar(Container container, String[] path) {
    if (path.length == 0) return null;

    int i = 0;
    while (true) {
        JMenuBar menuBar = findMenuBar(container, i);
        if (menuBar == null) {
            String s = path[0];
            for (int j = 1; j < path.length; j++) s = s + " > " + path[j];
            return null;
        }
        JMenuItem menuItem = findMenuItem(menuBar, path);
        if (menuItem != null) return menuItem;
        i++;
    }
}
 
Example 11
Source Project: jdk8u-jdk   Source File: WindowsRootPaneUI.java    License: GNU General Public License v2.0 5 votes vote down vote up
void altPressed(KeyEvent ev) {
    MenuSelectionManager msm =
        MenuSelectionManager.defaultManager();
    MenuElement[] path = msm.getSelectedPath();
    if (path.length > 0 && ! (path[0] instanceof ComboPopup)) {
        msm.clearSelectedPath();
        menuCanceledOnPress = true;
        ev.consume();
    } else if(path.length > 0) { // We are in ComboBox
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        ev.consume();
    } else {
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        JMenuBar mbar = root != null ? root.getJMenuBar() : null;
        if(mbar == null && winAncestor instanceof JFrame) {
            mbar = ((JFrame)winAncestor).getJMenuBar();
        }
        JMenu menu = mbar != null ? mbar.getMenu(0) : null;
        if(menu != null) {
            ev.consume();
        }
    }
}
 
Example 12
Source Project: jdk8u-jdk   Source File: MisplacedBorder.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws standard JMenuBar.
 */
private BufferedImage step1(final JMenuBar menubar) {
    final BufferedImage bi1 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);
    final Graphics2D g2d = bi1.createGraphics();
    g2d.scale(2, 2);
    g2d.setColor(Color.RED);
    g2d.fillRect(0, 0, W, H);
    menubar.paintAll(g2d);
    g2d.dispose();
    return bi1;
}
 
Example 13
/**
 * Maps {@code JMenuBar.menuSelectionChanged(boolean)} through queue
 */
public void menuSelectionChanged(final boolean b) {
    runMapping(new MapVoidAction("menuSelectionChanged") {
        @Override
        public void map() {
            ((JMenuBar) getSource()).menuSelectionChanged(b);
        }
    });
}
 
Example 14
Source Project: blog-codes   Source File: BasicGraphEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 
 */
public JFrame createFrame(JMenuBar menuBar)
{
	JFrame frame = new JFrame();
	frame.getContentPane().add(this);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setJMenuBar(menuBar);
	frame.setSize(870, 640);

	// Updates the frame title
	updateTitle();

	return frame;
}
 
Example 15
Source Project: FlatLaf   Source File: FlatMenuItemBorder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Insets getBorderInsets( Component c, Insets insets ) {
	if( c.getParent() instanceof JMenuBar ) {
		insets.top = scale( menuBarItemMargins.top );
		insets.left = scale( menuBarItemMargins.left );
		insets.bottom = scale( menuBarItemMargins.bottom );
		insets.right = scale( menuBarItemMargins.right );
		return insets;
	} else
		return super.getBorderInsets( c, insets );
}
 
Example 16
Source Project: FlatLaf   Source File: FlatTitlePane.java    License: Apache License 2.0 5 votes vote down vote up
protected void addSubComponents() {
	leftPanel = new JPanel();
	iconLabel = new JLabel();
	titleLabel = new JLabel();
	iconLabel.setBorder( new FlatEmptyBorder( UIManager.getInsets( "TitlePane.iconMargins" ) ) );
	titleLabel.setBorder( new FlatEmptyBorder( UIManager.getInsets( "TitlePane.titleMargins" ) ) );

	leftPanel.setLayout( new BoxLayout( leftPanel, BoxLayout.LINE_AXIS ) );
	leftPanel.setOpaque( false );
	leftPanel.add( iconLabel );

	menuBarPlaceholder = new JComponent() {
		@Override
		public Dimension getPreferredSize() {
			JMenuBar menuBar = rootPane.getJMenuBar();
			return (menuBar != null && isMenuBarEmbedded())
				? FlatUIUtils.addInsets( menuBar.getPreferredSize(), UIScale.scale( menuBarMargins ) )
				: new Dimension();
		}
	};
	leftPanel.add( menuBarPlaceholder );

	createButtons();

	setLayout( new BorderLayout() );
	add( leftPanel, BorderLayout.LINE_START );
	add( titleLabel, BorderLayout.CENTER );
	add( buttonPanel, BorderLayout.LINE_END );
}
 
Example 17
/**
 * Maps {@code JMenuBar.setMargin(Insets)} through queue
 */
public void setMargin(final Insets insets) {
    runMapping(new MapVoidAction("setMargin") {
        @Override
        public void map() {
            ((JMenuBar) getSource()).setMargin(insets);
        }
    });
}
 
Example 18
Source Project: netbeans   Source File: MenuEditLayer.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean containsMenuTypeComponent(RADVisualComponent[] comps) {
    if(comps == null) return false;
    if(comps.length < 1) return false;
    for(RADVisualComponent c : comps) {
        if(JMenuItem.class.isAssignableFrom(c.getBeanClass())) return true;
        if(JMenuBar.class.isAssignableFrom(c.getBeanClass())) return true;
        if(JMenu.class.isAssignableFrom(c.getBeanClass())) return true;
    }
    return false;
}
 
Example 19
Source Project: jdk8u_jdk   Source File: MisplacedBorder.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void run() {
    final JMenuBar menubar = new JMenuBar();
    menubar.add(new JMenu(""));
    menubar.add(new JMenu(""));
    final JFrame frame = new JFrame();
    frame.setUndecorated(true);
    frame.setJMenuBar(menubar);
    frame.setSize(W / 3, H / 3);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    // draw menu bar using standard order.
    final BufferedImage bi1 = step1(menubar);

    // draw menu border on top of the menu bar, nothing should be changed.
    final BufferedImage bi2 = step2(menubar);
    frame.dispose();

    for (int x = 0; x < W; ++x) {
        for (int y = 0; y < H; ++y) {
            if (bi1.getRGB(x, y) != bi2.getRGB(x, y)) {
                try {
                    ImageIO.write(bi1, "png", new File("image1.png"));
                    ImageIO.write(bi2, "png", new File("image2.png"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                throw new RuntimeException("Failed: wrong color");
            }
        }
    }
}
 
Example 20
Source Project: slick2d-maven   Source File: Hiero.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void initializeMenus () {
	{
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		{
			JMenu fileMenu = new JMenu();
			menuBar.add(fileMenu);
			fileMenu.setText("File");
			fileMenu.setMnemonic(KeyEvent.VK_F);
			{
				openMenuItem = new JMenuItem("Open Hiero settings file...");
				openMenuItem.setMnemonic(KeyEvent.VK_O);
				openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_MASK));
				fileMenu.add(openMenuItem);
			}
			{
				saveMenuItem = new JMenuItem("Save Hiero settings file...");
				saveMenuItem.setMnemonic(KeyEvent.VK_S);
				saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK));
				fileMenu.add(saveMenuItem);
			}
			fileMenu.addSeparator();
			{
				saveBMFontMenuItem = new JMenuItem("Save BMFont files (text)...");
				saveBMFontMenuItem.setMnemonic(KeyEvent.VK_B);
				saveBMFontMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, KeyEvent.CTRL_MASK));
				fileMenu.add(saveBMFontMenuItem);
			}
			fileMenu.addSeparator();
			{
				exitMenuItem = new JMenuItem("Exit");
				exitMenuItem.setMnemonic(KeyEvent.VK_X);
				fileMenu.add(exitMenuItem);
			}
		}
	}
}
 
Example 21
Source Project: openjdk-jdk9   Source File: MisplacedBorder.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws standard JMenuBar.
 */
private BufferedImage step1(final JMenuBar menubar) {
    final BufferedImage bi1 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);
    final Graphics2D g2d = bi1.createGraphics();
    g2d.scale(2, 2);
    g2d.setColor(Color.RED);
    g2d.fillRect(0, 0, W, H);
    menubar.paintAll(g2d);
    g2d.dispose();
    return bi1;
}
 
Example 22
public static Object getSelectedElement(JMenuBar bar) {
    MenuElement[] subElements = bar.getSubElements();
    for (MenuElement subElement : subElements) {
        if (subElement instanceof JMenu
                && ((JMenu) subElement).isPopupMenuVisible()) {
            return subElement;
        }
    }
    return null;
}
 
Example 23
Source Project: netbeans   Source File: MenuEditLayer.java    License: Apache License 2.0 5 votes vote down vote up
private String getComponentDefaultsPrefix(JComponent c) {
    if(c instanceof JMenuBar) {
        return "MenuBar"; // NOI18N
    }
    if(c instanceof JMenu) {
        return "Menu"; // NOI18N
    }
    if(c instanceof JCheckBoxMenuItem) {
        return "CheckBoxMenuItem"; // NOI18N
    }
    if(c instanceof JRadioButtonMenuItem) {
        return "RadioButtonMenuItem"; // NOI18N
    }
    return "MenuItem"; // NOI18N
}
 
Example 24
/**
 * Maps {@code JMenuBar.getUI()} through queue
 */
public MenuBarUI getUI() {
    return (runMapping(new MapAction<MenuBarUI>("getUI") {
        @Override
        public MenuBarUI map() {
            return ((JMenuBar) getSource()).getUI();
        }
    }));
}
 
Example 25
Source Project: openjdk-jdk9   Source File: AppleMenuDriver.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void activateMenu(JMenuBar bar) {
    if (getSelectedElement(bar) == null) {
        tryToActivate();
        if (getSelectedElement(bar) == null) {
            tryToActivate();
        }
    }
}
 
Example 26
/**
 * Draws standard JMenuBar.
 */
private BufferedImage step1(final JMenuBar menubar) {
    final BufferedImage bi1 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);
    final Graphics2D g2d = bi1.createGraphics();
    g2d.scale(2, 2);
    g2d.setColor(Color.RED);
    g2d.fillRect(0, 0, W, H);
    menubar.paintAll(g2d);
    g2d.dispose();
    return bi1;
}
 
Example 27
Source Project: Cafebabe   Source File: Editor.java    License: GNU General Public License v3.0 5 votes vote down vote up
private JMenuBar createMenu() {
	JMenuBar mb = new JMenuBar();
	JMenu actions = new JMenu(Translations.get("Actions"));
	JMenuItem close = new JMenuItem(Translations.get("Close all"));
	close.addActionListener(l -> {
		pane.closeAll();
	});

	actions.add(close);
	mb.add(actions);
	return mb;
}
 
Example 28
@Override
public Hashtable<String, Object> getDump() {
    Hashtable<String, Object> result = super.getDump();
    String[] items = new String[((JMenuBar) getSource()).getMenuCount()];
    for (int i = 0; i < ((JMenuBar) getSource()).getMenuCount(); i++) {
        if (((JMenuBar) getSource()).getMenu(i) != null) {
            items[i] = ((JMenuBar) getSource()).getMenu(i).getText();
        } else {
            items[i] = "null";
        }
    }
    addToDump(result, SUBMENU_PREFIX_DPROP, items);
    return result;
}
 
Example 29
Source Project: astor   Source File: DateTimeBrowser.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void go(String[] args) {

        mainArgs = args;
        setDefaultTimeZone();   // let user override if needed
        // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        //
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar( menuBar );
        addMenus( menuBar );
        /*
         * Add a fast close listener
         */

        addWindowListener( new WindowAdapter() {
                    public void windowClosing(WindowEvent e)
                    {
                        setVisible( false );
                        dispose();
                        System.exit(0);
                    }
                }
            );

        //
        // Load current file, prime tables and JFrame.
        //
        currFile = new LoadedFile( mainArgs[0] );
        TableView tView = getDefaultTableView();
        resetDefaults( tView );
        //
        // Set max size at start, and display the window.
        //
        Dimension screenMax = Toolkit.getDefaultToolkit().getScreenSize();
        setSize ( screenMax );
        setVisible(true);
    }
 
Example 30
void altPressed(KeyEvent ev) {
    MenuSelectionManager msm =
        MenuSelectionManager.defaultManager();
    MenuElement[] path = msm.getSelectedPath();
    if (path.length > 0 && ! (path[0] instanceof ComboPopup)) {
        msm.clearSelectedPath();
        menuCanceledOnPress = true;
        ev.consume();
    } else if(path.length > 0) { // We are in ComboBox
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        ev.consume();
    } else {
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        JMenuBar mbar = root != null ? root.getJMenuBar() : null;
        if(mbar == null && winAncestor instanceof JFrame) {
            mbar = ((JFrame)winAncestor).getJMenuBar();
        }
        JMenu menu = mbar != null ? mbar.getMenu(0) : null;
        if(menu != null) {
            ev.consume();
        }
    }
}