Java JMenuBar ,JMenu and JMenuItem

Java JMenuBar ,JMenu and JMenuItem

Java JMenuBar
Java JMenuBar is used to add menubar to a JFrame window.JMenu are added to JMenuBar.JMenuItems are drop down menus that are added to JMenu.

How to Declare Java JMenuBar

[ class JMenu extends JMenuItem implements MenuElement, Accessible]

Java JMenuBar Example

[
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;

public class JavaJMenuBarExample  extends JFrame{
    public JMenuBar bar;
    public JMenuItem item1,item2,item3,item4,item5;
    public JMenu menu,sub;

    JavaJMenuBarExample ()
    {
        super("JavaJMenuBarExample ");
        try        {
            UIManager.setLookAndFee
                    ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
        }
        catch(Exception e)
        {

        }
        bar=new JMenuBar();
        menu=new JMenu("Main Menu");

        sub=new JMenu("Sub Menu");
        item1=new JMenuItem("Item 1");
        item2=new JMenuItem("Item 2");
        item3=new JMenuItem("Item 3");
        item4=new JMenuItem("Item 4");
        item5=new JMenuItem("Item 5");

        menu.add(item1);

        menu.add(item2);
        menu.add(item3);

        sub.add(item4);
        sub.add(item5);
        menu.add(sub);
        bar.add(menu);

        setJMenuBar(bar);
        setSize(400,400);
        setLocationRelativeTo(null);
        setLayout(null);
        setVisible(true);

    }

    public static void main(String args[])
    {
        new JavaJMenuBarExample();
    }



}
]

How to create a menu for a Notepad

[
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import javax.swing.JTextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JavaJMenuBarExample  extends JFrame
        implements ActionListener{
    public JMenuBar bar;
    public JMenuItem newFile,copy,cut,paste,selectAll;
    public JMenu edit,file,help;
    public  JTextArea area;

    JavaJMenuBarExample ()
    {
        super("JavaJMenuBarExample ");
        try        {
            UIManager.setLookAndFeel

                    ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
        }
        catch(Exception e)
        {

        }
        area=new JTextArea();
        area.setBounds(5,5,360,320);
        add(area);
        bar=new JMenuBar();
        edit=new JMenu("Edit");

        file=new JMenu("File");
        help=new JMenu("Help");

        copy=new JMenuItem("Copy");
        cut=new JMenuItem("Cut");
        paste=new JMenuItem("Paste");
        selectAll=new JMenuItem("Select All");

        file.addActionListener(this);
        copy.addActionListener(this);
        cut.addActionListener(this);
        paste.addActionListener(this);
        selectAll.addActionListener(this);


        edit.add(cut);

        edit.add(copy);
        edit.add(paste);

        edit.add(selectAll);

        bar.add(file);
        bar.add(edit);
        bar.add(help);

        setJMenuBar(bar);
        setSize(400,400);
        setLocationRelativeTo(null);
        setLayout(null);
        setVisible(true);

    }
    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource()==cut)
        {
            area.cut();

        }
        if(event.getSource()==paste)
        {
            area.paste();
        }
        if(event.getSource()==copy)
        {
            area.copy();
        }
        if(event.getSource()==selectAll)
        {
            area.selectAll();
        }
        if(event.getSource()==file)
        {
            new JavaJMenuBarExample();
        }
    }




    public static void main(String args[])
    {
        new JavaJMenuBarExample();
    }



}
]

Output

Java JMenuBar ,JMenu and JMenuItem

Code Explanation

Above class [ class JavaJMenuBarExample extends JFrame implements ActionListener{] implements ActionListener interface and extends JFrame class.
Once you extend a class,you can access all class method.
JFrame class methods are :-
  • [ setJMenuBar(bar);]-sets JFrame menu bar.
  • [setSize(400,400);]-sets JFrame size.
  • [setLocationRelativeTo(null);]-positions the JFrame at the center of the window.
  • [setLayout(null);]-sets no layout to JFrame.
  • [setVisible(true);]-makes JFrame window visible.
  • [setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);]-exits application when JFrame close button is clicked.
Code [ try {
            UIManager.setLookAndFeel

                    ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
        }
        catch(Exception e)
        {
 
        }]
sets setsLookandFeel to WindowsClassicLookAndFeel.

Code [public void actionPerformed(ActionEvent event)
    {
        if(event.getSource()==cut)
        {
            area.cut();

        }
        if(event.getSource()==paste)
        {
            area.paste();
        }
        if(event.getSource()==copy)
        {
            area.copy();
        }
        if(event.getSource()==selectAll)
        {
            area.selectAll();
        }
        if(event.getSource()==file)
        {
            new JavaJMenuBarExample();
        }
    }
]
Listens for actions performed on JMenuItems.The class implements ActionListener interface.


COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Java JMenuBar ,JMenu and JMenuItem
Java JMenuBar ,JMenu and JMenuItem
Java JMenuBar ,JMenu and JMenuItem
https://2.bp.blogspot.com/-VowJmp0aaLo/WMqYxA5-KlI/AAAAAAAAHMw/fnh6EhXh1mMNpqtVokZ2A0Gx-4va19TgQCLcB/s200/java-jmenubar-example-min.PNG
https://2.bp.blogspot.com/-VowJmp0aaLo/WMqYxA5-KlI/AAAAAAAAHMw/fnh6EhXh1mMNpqtVokZ2A0Gx-4va19TgQCLcB/s72-c/java-jmenubar-example-min.PNG
Java Codes
http://www.download-all.net/2017/03/java-jmenubar-jmenu-and-jmenuitem.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/java-jmenubar-jmenu-and-jmenuitem.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy