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

Threaded View

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 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.

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

    niecah (April 30th, 2010)


Similar Threads

  1. Replies: 24
    Last Post: August 4th, 2014, 12:49 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