Example usage for javax.swing JMenuBar JMenuBar

List of usage examples for javax.swing JMenuBar JMenuBar

Introduction

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

Prototype

public JMenuBar() 

Source Link

Document

Creates a new menu bar.

Usage

From source file:Main.java

public JMenuBar createMenuBar() {
    JMenuBar top_menu_bar = new JMenuBar();
    JMenu main_menu = new JMenu("Menu");
    main_menu.setMnemonic(KeyEvent.VK_M);
    top_menu_bar.add(main_menu);/*w  ww  . ja v  a 2 s  . c  om*/
    JMenuItem menu_item;

    menu_item = new JMenuItem("Add New");
    menu_item.setMnemonic(KeyEvent.VK_N);
    menu_item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
    menu_item.setActionCommand("new");
    menu_item.addActionListener(e -> createThumb());
    main_menu.add(menu_item);
    return top_menu_bar;
}

From source file:MainClass.java

MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToolBar toolBar = new JToolBar();
    Action a = new AbstractAction("Demo") {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Action taken.");
        }/*from   w  w  w.  j  a va2 s  .  co  m*/
    };

    JButton b = toolBar.add(a);
    b.setText("Demo Button");
    b.setToolTipText("Press me to take action.");

    JMenu mainMenu = new JMenu("Menu");
    JMenuItem mi = mainMenu.add(a);
    mi.getAccessibleContext().setAccessibleName("Menu item");

    JMenuBar mb = new JMenuBar();
    mb.add(mainMenu);
    setJMenuBar(mb);

    JPanel pane = new JPanel();
    pane.setLayout(new BorderLayout());
    pane.setPreferredSize(new Dimension(200, 100));
    pane.add(toolBar, BorderLayout.NORTH);
    setContentPane(pane);

    pack();
    setVisible(true);
}

From source file:components.TopLevelDemo.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.//from  w w w .j ava  2 s  .  c om
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TopLevelDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create the menu bar.  Make it have a green background.
    JMenuBar greenMenuBar = new JMenuBar();
    greenMenuBar.setOpaque(true);
    greenMenuBar.setBackground(new Color(154, 165, 127));
    greenMenuBar.setPreferredSize(new Dimension(200, 20));

    //Create a yellow label to put in the content pane.
    JLabel yellowLabel = new JLabel();
    yellowLabel.setOpaque(true);
    yellowLabel.setBackground(new Color(248, 213, 131));
    yellowLabel.setPreferredSize(new Dimension(200, 180));

    //Set the menu bar and add the label to the content pane.
    frame.setJMenuBar(greenMenuBar);
    frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:ToolBarExample.java

public ToolBarExample() {
    menuBar = new JMenuBar();

    // Create a set of actions to use in both the menu and toolbar
    DemoAction leftJustifyAction = new DemoAction("Left", new ImageIcon("1.gif"), "Left justify text", 'L');
    DemoAction rightJustifyAction = new DemoAction("Right", new ImageIcon("2.gif"), "Right justify text", 'R');
    DemoAction centerJustifyAction = new DemoAction("Center", new ImageIcon("3.gif"), "Center justify text",
            'M');
    DemoAction fullJustifyAction = new DemoAction("Full", new ImageIcon("4.gif"), "Full justify text", 'F');

    JMenu formatMenu = new JMenu("Justify");
    formatMenu.add(leftJustifyAction);//from   w  ww  .ja  va 2 s .  com
    formatMenu.add(rightJustifyAction);
    formatMenu.add(centerJustifyAction);
    formatMenu.add(fullJustifyAction);

    menuBar.add(formatMenu);

    toolBar = new JToolBar("Formatting");
    toolBar.add(leftJustifyAction);
    toolBar.add(rightJustifyAction);
    toolBar.add(centerJustifyAction);
    toolBar.add(fullJustifyAction);

    toolBar.addSeparator();
    JLabel label = new JLabel("Font");
    toolBar.add(label);

    toolBar.addSeparator();
    JComboBox combo = new JComboBox(fonts);
    combo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                pane.getStyledDocument().insertString(0,
                        "Font [" + ((JComboBox) e.getSource()).getSelectedItem() + "] chosen!\n", null);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    toolBar.add(combo);

    //  Disable one of the Actions
    fullJustifyAction.setEnabled(false);
}

From source file:QandE.MyDemo2.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.//  ww w  .ja va2  s .  c  o  m
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("MyDemo2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenu menu = new JMenu("Menu");
    JMenuBar mb = new JMenuBar();
    mb.add(menu);
    frame.setJMenuBar(mb);

    //Add a label with bold italic font.
    JLabel label = new JLabel("My Demo");
    frame.getContentPane().add(BorderLayout.CENTER, label);
    if (true) {
        label.setFont(label.getFont().deriveFont(Font.ITALIC | Font.BOLD));
    } else {
        //another way of doing it, but not as appropriate since 
        //setFont is faster than using HTML.
        label.setText("<html><i>My Demo</i></html>");
    }
    label.setHorizontalAlignment(JLabel.CENTER);

    //Display the window, making it a little bigger than it really needs to be.
    frame.pack();
    frame.setSize(frame.getWidth() + 100, frame.getHeight() + 50);
    frame.setVisible(true);
}

From source file:SimpleMenus.java

public void init() {
    for (int i = 0; i < items.length; i++) {
        items[i].addActionListener(al);//ww  w. j  ava2  s .  c om
        menus[i % 3].add(items[i]);
    }
    JMenuBar mb = new JMenuBar();
    for (int i = 0; i < menus.length; i++)
        mb.add(menus[i]);
    setJMenuBar(mb);
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(t);
}

From source file:ColorChooserMenu.java

protected JMenuBar createMenuBar() {
    final JMenuBar menuBar = new JMenuBar();

    ColorMenu cm = new ColorMenu("Foreground");
    cm.setMnemonic('f');
    ActionListener lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ColorMenu m = (ColorMenu) e.getSource();
            System.out.println(m.getColor());
        }/*from  www  .  j a v  a  2 s.  c o  m*/
    };
    cm.addActionListener(lst);
    menuBar.add(cm);

    return menuBar;
}

From source file:ScreenCapture.java

public ScreenCapture(String title) {
    super(title);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JMenuBar mb = new JMenuBar();
    JMenu menu = new JMenu("File");
    ActionListener al;// w  w w. j av  a  2  s.  co  m

    JMenuItem mi = new JMenuItem("Save");
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            save();
        }
    };

    mi.addActionListener(al);
    menu.add(mi);
    mb.add(menu);

    menu = new JMenu("Capture");

    mi = new JMenuItem("Capture");
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setVisible(false);

            BufferedImage biScreen = robot.createScreenCapture(rectScreenSize);
            setVisible(true);

            ia.setImage(biScreen);

            jsp.getHorizontalScrollBar().setValue(0);
            jsp.getVerticalScrollBar().setValue(0);
        }
    };

    mi.addActionListener(al);
    menu.add(mi);

    mb.add(menu);

    mi = new JMenuItem("Crop");
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (ia.crop()) {
                jsp.getHorizontalScrollBar().setValue(0);
                jsp.getVerticalScrollBar().setValue(0);
            }
        }
    };

    mi.addActionListener(al);
    menu.add(mi);

    mb.add(menu);
    setJMenuBar(mb);
    getContentPane().add(jsp = new JScrollPane(ia));
    setVisible(true);
}

From source file:MainClass.java

public MainClass() {
    JFrame frame = new JFrame();
    JTextPane textPane = new JTextPane();
    JScrollPane scrollPane = new JScrollPane(textPane);

    JPanel north = new JPanel();

    JMenuBar menu = new JMenuBar();
    JMenu styleMenu = new JMenu();
    styleMenu.setText("Style");

    Action boldAction = new BoldAction();
    boldAction.putValue(Action.NAME, "Bold");
    styleMenu.add(boldAction);/*w  w  w.  j  a  v a2  s  .c o m*/

    Action italicAction = new ItalicAction();
    italicAction.putValue(Action.NAME, "Italic");
    styleMenu.add(italicAction);

    Action foregroundAction = new ForegroundAction();
    foregroundAction.putValue(Action.NAME, "Color");
    styleMenu.add(foregroundAction);

    Action formatTextAction = new FontAndSizeAction();
    formatTextAction.putValue(Action.NAME, "Font and Size");
    styleMenu.add(formatTextAction);

    menu.add(styleMenu);

    north.add(menu);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(north, BorderLayout.NORTH);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(800, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Java2DExample.java

public Java2DExample() {
    super("Java 2D Image Processing Demo");
    imagePanel = new ImagePanel(Java2DExample.class.getResource("yourImage.png"));

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);/*from  w ww .ja v a 2 s  .  c  o  m*/
    filterMenu.setMnemonic('I');

    JMenuItem originalMenuItem = new JMenuItem("Display Original");
    originalMenuItem.setMnemonic('O');

    originalMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent action) {
            imagePanel.displayOriginalImage();
        }

    });

    JMenuItem invertMenuItem = createMenuItem("Invert", 'I', invertFilter);
    JMenuItem sharpenMenuItem = createMenuItem("Sharpen", 'S', sharpenFilter);
    JMenuItem blurMenuItem = createMenuItem("Blur", 'B', blurFilter);
    JMenuItem changeColorsMenuItem = createMenuItem("Change Colors", 'C', colorFilter);

    filterMenu.add(originalMenuItem);
    filterMenu.add(invertMenuItem);
    filterMenu.add(sharpenMenuItem);
    filterMenu.add(blurMenuItem);
    filterMenu.add(changeColorsMenuItem);

    menuBar.add(filterMenu);

    getContentPane().add(imagePanel, BorderLayout.CENTER);

}