JFrame:setJMenuBar(JMenuBar bar) Example

JFrame:setJMenuBar(JMenuBar bar) Example

JFrame:setJMenuBar(JMenuBar bar) Example
JFrame window has several methods.These include setSize,setLocation, add etc.setJMenuBar is a method that is used to top menu navigation to a JFrame.Below code explains


How to use JFrame:setJMenuBar(JMenuBar bar) method.

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

public class JFrameSetJMenuBarExample extends JFrame {
    JFrameSetJMenuBarExample()
    {
        super("JMenuBar Example");

        JMenuBar bar=new JMenuBar();
        JMenu menu=new JMenu("File");
        JMenuItem save=new JMenuItem("Save");
        JMenuItem edit=new JMenuItem("Edit");
        menu.add(save);
        menu.add(edit);
        bar.add(menu);
        setJMenuBar(bar);
        setSize(400,400);
        setLocationRelativeTo(null);
        setVisible(true);

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

}
]

Adding Action Listener to JMenuItems

After adding JMenuItems to JMenu, the other thing you are supposed to do is addActionListner to these JMenuItems.Fire up your IDE and execute below code.
[
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JFrameSetJMenuBarExample extends JFrame
        implements ActionListener {
    public    JMenuItem save,edit;
    JFrameSetJMenuBarExample()
    {
        super("JMenuBar Example");

        JMenuBar bar=new JMenuBar();
        JMenu menu=new JMenu("File");
        save=new JMenuItem("Save");
        edit=new JMenuItem("Edit");
        save.addActionListener(this);
        edit.addActionListener(this);

        menu.add(save);
        menu.add(edit);
        bar.add(menu);
        setJMenuBar(bar);
        setSize(400,400);
        setLocationRelativeTo(null);
        setVisible(true);

    }
    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource()==save)
        {
            JOptionPane.showMessageDialog(this, "Menu selected is Save",
                    "JMenu Example",JOptionPane.INFORMATION_MESSAGE);
        }
        if(event.getSource()==edit)
        {
            JOptionPane.showMessageDialog(this, "Menu selected is Edit",
                    "JMenu Example",JOptionPane.INFORMATION_MESSAGE);
        }

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

}
]

Code Explanation:

Above class extends JFrame and implements interface ActionListener.

What does it mean when a class extends another class?

If a class extends another class, all non-abstract methods contained in the parent class becomes accessible to the child class.The child class can just call the directly or ovveride them.

What it means when a class implements a certain interface?

If a class implements a certain interface,its the obligation of the class to override all the non-abstract interface methods.

In our case here,JFrame methods that become accessible to our class here are
[bar.add(menu);
setJMenuBar(bar);
setSize(400,400);
setLocationRelativeTo(null);
setVisible(true);]

ActionListener interface method that we need to override is [public void actionPerformed(ActionEvent event)
{
    if(event.getSource()==save)
    {
        JOptionPane.showMessageDialog(this, "Menu selected is Save",
                "JMenu Example",JOptionPane.INFORMATION_MESSAGE);
    }
    if(event.getSource()==edit)
    {
        JOptionPane.showMessageDialog(this, "Menu selected is Edit",
                "JMenu Example",JOptionPane.INFORMATION_MESSAGE);
    }
}]

Other Swing components you should Check out.

JOptionPane:showMessageDialog




COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: JFrame:setJMenuBar(JMenuBar bar) Example
JFrame:setJMenuBar(JMenuBar bar) Example
JFrame:setJMenuBar(JMenuBar bar) Example
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNU-YwPYsh2DsPEi6I5FCkzL8v5FAvm8XshM8Gxp2NjhIQOMJz0D327_LferaZYlMRpkcGzbKMcwmhFAC-8RJWnhP6htncMV7F5i6EmtL4X5urCABp4fEaqfuBO6Yef94qmJc2p_mN7YyD/s200/Jframe-setJmenubar-method-example.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNU-YwPYsh2DsPEi6I5FCkzL8v5FAvm8XshM8Gxp2NjhIQOMJz0D327_LferaZYlMRpkcGzbKMcwmhFAC-8RJWnhP6htncMV7F5i6EmtL4X5urCABp4fEaqfuBO6Yef94qmJc2p_mN7YyD/s72-c/Jframe-setJmenubar-method-example.PNG
Code WHIZZ
https://code-whizz.blogspot.com/2017/03/jframesetjmenubarjmenubar-bar-example.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/03/jframesetjmenubarjmenubar-bar-example.html
true
5534598864497432585
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