Java program to Add a JMenu toolbar to a Java Swing application
With this code you can add a JMenu toolbar to your Java Swing applications.
http://javaprogrammingforums.com/images/jmenu1.jpg
Code :
import java.awt.event.*;
import javax.swing.*;
public class JMenuExample extends JFrame implements ActionListener {
public static void main(String[] s) {
new JMenuExample();
}
public JMenuExample() {
super("JMenu Example");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Name the JMenu & Add Items
JMenu menu = new JMenu("File");
menu.add(makeMenuItem("Open"));
menu.add(makeMenuItem("Save"));
menu.add(makeMenuItem("Quit"));
// Add JMenu bar
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
setJMenuBar(menuBar);
setSize(300, 300);
setLocation(200, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// Menu item actions
String command = e.getActionCommand();
if (command.equals("Quit")) {
System.exit(0);
} else if (command.equals("Open")) {
// Open menu item action
System.out.println("Open menu item clicked");
} else if (command.equals("Save")) {
// Save menu item action
System.out.println("Save menu item clicked");
}
}
private JMenuItem makeMenuItem(String name) {
JMenuItem m = new JMenuItem(name);
m.addActionListener(this);
return m;
}
}
Re: How to Add a JMenu toolbar to a Java Swing application
Nice tutorial. Although I would like to point out that this is not the only way to do it,but an efficient way nonetheless.
Re: How to Add a JMenu toolbar to a Java Swing application
Quote:
Originally Posted by
Fendaril
Nice tutorial. Although I would like to point out that this is not the only way to do it,but an efficient way nonetheless.
Thanks Fendaril but I would also like to thank John for this code.
You are correct, this is not the only way to do it. If you would like to post some alternative solutions that would be great!! :)
Re: How to Add a JMenu toolbar to a Java Swing application
yey!! this is what i'm lloking for!!! tnx tnx tnx!!!
Re: How to Add a JMenu toolbar to a Java Swing application
Menu mnemonics can be used to open a menu by typing a single character associated with a menu along with an operating system defined key for this action. e.g. fileMenu.setMnemonic('F');
Menu item mnemonics are used to select a menu item when its menu is already open.
Accelerator key combinations are used to directly invoke a menu item without opening the menu, for example the common CTRL-C (Copy) execute the copy menu action. e.g. openItem.setAccelerator(KeyStroke.getKeyStroke("co ntrol O")); // control O for open, control C for copy.
Re: How to Add a JMenu toolbar to a Java Swing application
thank you Mr.javaPF
i like the lesson
I have another code
Code :
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class menuDemo extends JFrame implements ActionListener{
public static final int width = 300;
public static final int height = 300;
public static void main(String[] args)
{
menuDemo gui = new menuDemo();
gui.setVisible(true);
}
public menuDemo()
{
super("Menu bar :");
setSize( width, height );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenu fileMenu = new JMenu("File");
JMenuItem open = new JMenuItem("Open");
open.addActionListener(this);
fileMenu.add(open);
JMenuItem save = new JMenuItem("Save");
save.addActionListener(this);
fileMenu.add(save);
JMenuItem quit = new JMenuItem("Quit");
quit.addActionListener(this);
fileMenu.add(quit);
JMenuBar bar = new JMenuBar();
bar.add(fileMenu);
setJMenuBar(bar);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Quit")) {
System.exit(0);
} else if (command.equals("Open")) {
System.out.println("Open menu item clicked");
} else if (command.equals("Save")) {
System.out.println("Save menu item clicked");
}
}
}
I hope you like it
Re: How to Add a JMenu toolbar to a Java Swing application
Thanks for the nice example :)