Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Java program to Add a JMenu toolbar to a Java Swing application

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Post 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.



    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;
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  2. The Following User Says Thank You to JavaPF For This Useful Post:

    niecah (April 30th, 2010)


  3. #2
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default 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.

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Thumbs up Re: How to Add a JMenu toolbar to a Java Swing application

    Quote Originally Posted by Fendaril View Post
    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!!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    17
    My Mood
    Confused
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: How to Add a JMenu toolbar to a Java Swing application

    yey!! this is what i'm lloking for!!! tnx tnx tnx!!!

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Location
    Mumbai
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to Add a JMenu toolbar to a Java Swing application

    thank you Mr.javaPF
    i like the lesson

    I have another 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

  8. The Following User Says Thank You to sciences For This Useful Post:

    JavaPF (March 9th, 2012)

  9. #7
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: How to Add a JMenu toolbar to a Java Swing application

    Thanks for the nice example

Similar Threads

  1. Replies: 23
    Last Post: July 4th, 2014, 06:53 PM
  2. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  3. How to Sendkeys to an application in Java using the Robot Class
    By JavaPF in forum Java SE API Tutorials
    Replies: 6
    Last Post: August 4th, 2011, 12:13 AM

Tags for this Thread