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.