Is there a way to associate a menu bar with the sketch frame?

I'm trying to add a menu bar to my sketch frame. I found the code below ( http://www.java-tips.org/java-se-tips-100019/15-javax-swing/1755-how-to-create-menu-bar.html ) and it would work to have some sort of menu bar, but I would really like to attach it to the frame. I found some old code of how to attach a menu bar., but it no longer works. Even after updating the calls like JMenu etc. , it breaks on the frame.setJMenuBar(myMenu) say the Jmenubar iss not applicable to that method.

Is it possible to modify this code to have the menu bar associated with frame?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

public class MenuExp extends JFrame {

    public MenuExp() {

        setTitle("Menu Example");
        setSize(400, 50);

        // Creates a menubar for a JFrame
        JMenuBar menuBar = new JMenuBar();

        // Add the menubar to the frame
        setJMenuBar(menuBar);

        // Define and add two drop down menu to the menubar
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);

        // Create and add simple menu item to one of the drop down menu
        JMenuItem newAction = new JMenuItem("New");
        JMenuItem openAction = new JMenuItem("Open");
        JMenuItem exitAction = new JMenuItem("Exit");
        JMenuItem cutAction = new JMenuItem("Cut");
        JMenuItem copyAction = new JMenuItem("Copy");
        JMenuItem pasteAction = new JMenuItem("Paste");

        // Create and add CheckButton as a menu item to one of the drop down
        // menu
        JCheckBoxMenuItem checkAction = new JCheckBoxMenuItem("Check Action");
        // Create and add Radio Buttons as simple menu items to one of the drop
        // down menu
        JRadioButtonMenuItem radioAction1 = new JRadioButtonMenuItem(
                "Radio Button1");
        JRadioButtonMenuItem radioAction2 = new JRadioButtonMenuItem(
                "Radio Button2");
        // Create a ButtonGroup and add both radio Button to it. Only one radio
        // button in a ButtonGroup can be selected at a time.
        ButtonGroup bg = new ButtonGroup();
        bg.add(radioAction1);
        bg.add(radioAction2);
        fileMenu.add(newAction);
        fileMenu.add(openAction);
        fileMenu.add(checkAction);
        fileMenu.addSeparator();
        fileMenu.add(exitAction);
        editMenu.add(cutAction);
        editMenu.add(copyAction);
        editMenu.add(pasteAction);
        editMenu.addSeparator();
        editMenu.add(radioAction1);
        editMenu.add(radioAction2);
        // Add a listener to the New menu item. actionPerformed() method will
        // invoked, if user triggred this menu item
        newAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("You have clicked on the new action");
            }
        });
    }
 /*   public static void main(String[] args) {
        MenuExp me = new MenuExp();
        me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        me.setVisible(true);
    } */
}
      MenuExp me = new MenuExp();

void setup(){
     size(600,600);
      me.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
      me.setVisible(true);

}

void draw(){
     background(100);
}
Tagged:

Answers

  • Here's the old code I found. It breaks on frame.setMenuBar(myMenu);
    https://processing.org/discourse/beta/num_1214314785.html

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ButtonGroup;
    import javax.swing.JCheckBoxMenuItem;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JRadioButtonMenuItem;
    import processing.core.PApplet;
    
    JMenuBar myMenu;
    JMenu topButton;
    JMenuItem item1,item2,item3,item4,item5;
    
    myMenuListener menuListen;
    
    color bg = color(255);
    
    void setup(){
     size(400,200);
     //this doesn't demonstrate best coding practice, just a simple method
     //create the MenuBar Object
     menuListen = new myMenuListener();
     myMenu = new JMenuBar();
    
     //create the top level button
     topButton = new JMenu("Colours");
    
     //create all the Menu Items and add the menuListener to check their state.
     item1 = new JMenuItem("Red");
     item1.addActionListener(menuListen);
    
     item2 = new JMenuItem("Green");
     item2.addActionListener(menuListen);
     item3 = new JMenuItem("Blue");
     item3.addActionListener(menuListen);
     item4 = new JMenuItem("Yellow");
     item4.addActionListener(menuListen);
     item5 = new JMenuItem("Black");
     item5.addActionListener(menuListen);
    
     //add the items to the top level Button
     topButton.add(item1);
     topButton.add(item2);
     topButton.add(item3);
     topButton.add(item4);
     topButton.add(item5);
    
     //add the button to the menu
     myMenu.add(topButton);
    
     //add the menu to the frame!
     frame.setMenuBar(myMenu);   // and it breaks here
    
    }
    
    void draw(){
     // get the current menu state
     background(bg);
    }
    
    //this menuListener object is largely ripped off from http://java.sun.com/docs/books/tutorial/uiswing/examples/components/MenuDemoProject/src/components/MenuDemo.java
    
    class myMenuListener implements ActionListener, ItemListener{
    
     myMenuListener(){
    
     }
    
     public void actionPerformed(ActionEvent e) {
       MenuItem source = (MenuItem)(e.getSource());
       String s = "Action event detected."
         + "    Event source: " + source.getLabel()
         + " (an instance of " + getClassName(source) + ")";
       println(s);
    
       //this part changes the background colour
       if(source.getLabel().equals("Red")){
         bg = color(220,50,0);
       }
       if(source.getLabel().equals("Blue")){
         bg = color(30,100,255);
       }
       if(source.getLabel().equals("Green")){
         bg = color(40,200,0);
       }
       if(source.getLabel().equals("Yellow")){
         bg = color(220,220,0);
       }
       if(source.getLabel().equals("Black")){
         bg = color(0);
       }
     }
    
     public void itemStateChanged(ItemEvent e) {
           MenuItem source = (MenuItem)(e.getSource());
           String s = "Item event detected."
                      + "    Event source: " + source.getLabel()
                      + " (an instance of " + getClassName(source) + ")"
                      + "    New state: "
                      + ((e.getStateChange() == ItemEvent.SELECTED) ?
                        "selected":"unselected");
           println(s);
       }
    
    
    }
    
    //gets the class name of an object
    protected String getClassName(Object o) {
     String classString = o.getClass().getName();
     int dotIndex = classString.lastIndexOf(".");
     return classString.substring(dotIndex+1);
    }
    
  • edited August 2015

    I got the menu bar to appear. frame is of type Frame not JFrame . Now I have to try to work out the listener issue. When I uncomment it, its giving me an error I dont't even understand. "myMenuListener must implement the inherited abstract method ItemListener.itemStateChanged(ItemEvent) "

    I'll need to reed a few books on java to understand that error massage. Is there something I'm not importing that could save me all that reading?

    I've edited it some more - got rid of the error and this works. I think I can use this.

        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import javax.swing.event.MenuListener;
        import java.awt.MenuBar;
        import java.awt.Menu;
        import java.awt.MenuItem;
        import java.awt.event.ItemListener;
        import java.awt.event.ActionListener;
    
        MenuBar myMenu;
        Menu topButton;
        MenuItem item1,item2,item3,item4,item5;
    
         myMenuListener menuListen;
    
        color bg = color(255);
    
        void setup(){
         size(400,200);
         //this doesn't demonstrate best coding practice, just a simple method
         //create the MenuBar Object
         menuListen = new myMenuListener();
         myMenu = new MenuBar();
    
         //create the top level button
         topButton = new Menu("Colours");
    
         //create all the Menu Items and add the menuListener to check their state.
         item1 = new MenuItem("Red");
         item1.addActionListener(menuListen);
    
         item2 = new MenuItem("Green");
         item2.addActionListener(menuListen);
         item3 = new MenuItem("Blue");
         item3.addActionListener(menuListen);
         item4 = new MenuItem("Yellow");
         item4.addActionListener(menuListen);
         item5 = new MenuItem("Black");
         item5.addActionListener(menuListen);
    
        // add the items to the top level Button
         topButton.add(item1);
         topButton.add(item2);
         topButton.add(item3);
         topButton.add(item4);
         topButton.add(item5);
    
        // add the button to the menu
         myMenu.add(topButton);
    
         //add the menu to the frame!
         frame.setMenuBar(myMenu);   // and it breaks here
    
        }
    
        void draw(){
         // get the current menu state
         background(bg);
        }
    
        //this menuListener object is largely ripped off from http://java.sun.com/docs/books/tutorial/uiswing/examples/components/MenuDemoProject/src/components/MenuDemo.java
    
         class myMenuListener implements ActionListener{
    
         myMenuListener(){
    
         }
    
         public void actionPerformed(ActionEvent e) {
           MenuItem source = (MenuItem)(e.getSource());
           String s = "Action event detected."
             + "    Event source: " + source.getLabel()
             + " (an instance of " + getClassName(source) + ")";
           println(s);
    
           //this part changes the background colour
           if(source.getLabel().equals("Red")){
             bg = color(220,50,0);
           }
           if(source.getLabel().equals("Blue")){
             bg = color(30,100,255);
           }
           if(source.getLabel().equals("Green")){
             bg = color(40,200,0);
           }
           if(source.getLabel().equals("Yellow")){
             bg = color(220,220,0);
           }
           if(source.getLabel().equals("Black")){
             bg = color(0);
           }
         }
    
        /* public void itemStateChanged(ItemEvent e){
               MenuItem source =(MenuItem)(e.getSource());
               String s = "Item event detected."
                          + "    Event source: " + source.getLabel()
                          + " (an instance of " + getClassName(source) + ")"
                          + "    New state: "
                          + ((e.getStateChange() == ItemEvent.SELECTED) ?
                            "selected":"unselected");
               println(s);*
           } */
    
    
        }
    
        //gets the class name of an object
        protected String getClassName(Object o) {
         String classString = o.getClass().getName();
         int dotIndex = classString.lastIndexOf(".");
         return classString.substring(dotIndex+1);
        } 
    
  • Answer ✓

    I have it working now. This puts a menu bar on your sketch:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.event.MenuListener;
    import java.awt.MenuBar;
    import java.awt.Menu;
    import java.awt.MenuItem;
    import java.awt.event.ItemListener;
    
    
    MenuBar myMenu;
    Menu fileMenu,viewMenu,freqMenu;
    MenuItem fileLoad,fileSave,viewOpen,viewClose,viewFlip,freqSplit,freqJoin,freqSplitMethod;
    
    myMenuListener menuListen;
    
    color bg = color(255);
    
    void setup(){
     size(600,600);
     //this doesn't demonstrate best coding practice, just a simple method
     //create the MenuBar Object
     menuListen = new myMenuListener();
     myMenu = new MenuBar();
    
     //create the top level button
     fileMenu = new Menu("File");
     viewMenu = new Menu("View");
     freqMenu = new Menu ("Frequency");
    
     //create all the Menu Items and add the menuListener to check their state.
     fileLoad = new MenuItem("Load Layer");
     fileLoad.addActionListener(menuListen);
     fileSave = new MenuItem("Save Layer");
     fileSave.addActionListener(menuListen);
     viewOpen = new MenuItem("View Open");
     viewOpen.addActionListener(menuListen);
     viewClose = new MenuItem("View Close");
     viewClose.addActionListener(menuListen);
     viewFlip = new MenuItem("Flip View");
     viewFlip.addActionListener(menuListen);
     freqSplit = new MenuItem("Split Frequencies");
     freqSplit.addActionListener(menuListen);
     freqJoin = new MenuItem("Join Frequencies");
     freqJoin.addActionListener(menuListen);
     freqSplitMethod = new  MenuItem("Split Method");
    
     fileMenu.add(fileLoad);
     fileMenu.add(fileSave);
     viewMenu.add(viewOpen);
     viewMenu.add(viewClose);
     viewMenu.add(viewFlip);
     freqMenu.add(freqSplit);
     freqMenu.add(freqJoin);
     freqMenu.add(freqSplitMethod);
    
    
    
    // add the button to the menu
     myMenu.add(fileMenu);
     myMenu.add(viewMenu);
     myMenu.add(freqMenu);
    
     //add the menu to the frame!
     frame.setMenuBar(myMenu);   // and it breaks here
    
    }
    
    void draw(){
     // get the current menu state
     background(bg);
     point(0,0);
    }
    
    //this menuListener object is largely ripped off from http://java.sun.com/docs/books/tutorial/uiswing/examples/components/MenuDemoProject/src/components/MenuDemo.java
    
     class myMenuListener implements ActionListener{
    
     myMenuListener(){
    
     }
    
     public void actionPerformed(ActionEvent e) {
       MenuItem source = (MenuItem)(e.getSource());
       String s = "Action event detected."
         + "    Event source: " + source.getLabel()
         + " (an instance of " + getClassName(source) + ")";
       println(s);
    
       //this part changes the background colour
       if(source.getLabel().equals("Load Layer")){
         println("Load a layer");
       }
       else if(source.getLabel().equals("Save Layer")){
         println("Save a layer");
       }
       else if(source.getLabel().equals("View Open")){
         println(" Open view window");
       }
       else if(source.getLabel().equals("View Close")){
         println("Close view window");
       }
       else if(source.getLabel().equals("Flip View")){
         println("Flip view window");
       }
       else println(" etc. etc..");
    
     }
    
    
    
    }
    
    //gets the class name of an object
    protected String getClassName(Object o) {
     String classString = o.getClass().getName();
     int dotIndex = classString.lastIndexOf(".");
     return classString.substring(dotIndex+1);
    } 
    
  • And this fixes it completely. I guess itemStateChanged is in a file I wasn't importing and the wild card imports them all:

    import java.awt.*;
    import java.awt.event.*;
    
    MenuBar myMenu;
    Menu fileMenu,viewMenu,freqMenu,freqSMenu;
    MenuItem fileLoad,fileSave,viewOpen,viewClose,viewFlip,freqSplit, freqJoin, splitBlur,splitMed;
    
    myMenuListener menuListen;
    
    color bg = color(255);
    
    void setup(){
         size(600,600);
         menu_setup();
    }
    
    void draw(){
     // get the current menu state
     background(bg);
     point(0,0);
    }
    
    //this menuListener object is largely ripped off from http://java.sun.com/docs/books/tutorial/uiswing/examples/components/MenuDemoProject/src/components/MenuDemo.java
    
     class myMenuListener implements ActionListener, ItemListener{
    
     myMenuListener(){
    
     }
    
     public void actionPerformed(ActionEvent e) {
       MenuItem source = (MenuItem)(e.getSource());
       String s = "Action event detected."
         + "    Event source: " + source.getLabel()
         + " (an instance of " + getClassName(source) + ")";
       println(s);
    
       //this part changes the background colour
       if(source.getLabel().equals("Load Layer")){
         println("Load a layer");
       }
       else if(source.getLabel().equals("Save Layer")){
         println("Save a layer");
       }
       else if(source.getLabel().equals("View Open")){
         println(" Open view window");
       }
       else if(source.getLabel().equals("View Close")){
         println("Close view window");
       }
       else if(source.getLabel().equals("Flip View")){
         println("Flip view window");
       }
       else println(" etc. etc..");
    
     }
    
       public void itemStateChanged(ItemEvent e) {
    
      MenuItem source = (MenuItem)(e.getSource());
       String s = "Action event detected."
         + "    Event source: " + source.getLabel()
         + " (an instance of " + getClassName(source) + ")";
       println(s);
    
       //this part changes the background colour
       if(source.getLabel().equals("Load Layer")){
         println("Load a layer");
       }
       else if(source.getLabel().equals("Save Layer")){
         println("Save a layer");
       }
       else if(source.getLabel().equals("View Open")){
         println(" Open view window");
       }
       else if(source.getLabel().equals("View Close")){
         println("Close view window");
       }
       else if(source.getLabel().equals("Flip View")){
         println("Flip view window");
       }
       else println(" etc. etc..");
    
    
          }
    
    }
    
    
    
    protected String getClassName(Object o) {
          String classString = o.getClass().getName();
          int dotIndex = classString.lastIndexOf(".");
          return classString.substring(dotIndex+1);
    } 
    
    void menu_setup(){
    
     //this doesn't demonstrate best coding practice, just a simple method
     //create the MenuBar Object
     menuListen = new myMenuListener();
     myMenu = new MenuBar();
    
     //create the top level button
     fileMenu = new Menu("File");
     viewMenu = new Menu("View");
     freqMenu = new Menu ("Frequency");
     freqSMenu  = new Menu("Frequency Split");
    
     //create all the Menu Items and add the menuListener to check their state.
     fileLoad = new MenuItem("Load Layer");
     fileLoad.addActionListener(menuListen);
     fileSave = new MenuItem("Save Layer");
     fileSave.addActionListener(menuListen);
     viewOpen = new MenuItem("View Open");
     viewOpen.addActionListener(menuListen);
     viewClose = new MenuItem("View Close");
     viewClose.addActionListener(menuListen);
     viewFlip = new MenuItem("Flip View");
     viewFlip.addActionListener(menuListen);
     freqJoin = new MenuItem("Join Frequencies");
     freqJoin.addActionListener(menuListen);
     splitBlur = new MenuItem("Blur");
     splitBlur.addActionListener(menuListen);
     splitMed = new MenuItem("Median");
     splitMed.addActionListener(menuListen);
    
     fileMenu.add(fileLoad);
     fileMenu.add(fileSave);
     viewMenu.add(viewOpen);
     viewMenu.add(viewClose);
     viewMenu.add(viewFlip);
     freqMenu.add(freqSMenu);
     freqMenu.add(freqJoin);
     freqSMenu.add(splitBlur);
     freqSMenu.add(splitMed);
    
    
    
    // add the button to the menu
     myMenu.add(fileMenu);
     myMenu.add(viewMenu);
     myMenu.add(freqMenu);
    
     //add the menu to the frame!
     frame.setMenuBar(myMenu);  
    }
    
  • Next up - add scroll bars to the frame :)

Sign In or Register to comment.