# Is there a way to add JMenuBar (File, Edit...) to my program?

**URL:** https://discourse.processing.org/t/is-there-a-way-to-add-jmenubar-file-edit-to-my-program/31830
**Category:** Project Guidance
**Created:** [August 21, 2021, 5:17pm UTC](https://discourse.processing.org/t/is-there-a-way-to-add-jmenubar-file-edit-to-my-program/31830 "2021-08-21T17:17:53Z")
**Posts on this page:** 1
**Showing post:** 10

<div class="post-metadata">

### Author: ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)
#### Post date: [August 21, 2021, 8:23pm UTC](https://discourse.processing.org/t/is-there-a-way-to-add-jmenubar-file-edit-to-my-program/31830/10 "2021-08-21T20:23:48Z")

</div>

This is also a possibility, thank you for your efforts 😄

I just found how to create a menu:

> [@Show an desktop menu bar](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/2):
>
> The trick here is not to extend JFrame to create the menubar, rather use the one created by Processing for the sketch. This will not work with P2D and P3D because they use OpenGL so the sketch is shown in a GLWindow not a JFrame Menu\_bar mp; void setup() { buildMenuBar(); } void draw() { } void buildMenuBar() { mp = new Menu\_bar(this, "Media", 100, 100); } Menubar tab import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing…

I’ve revised that a bit:

**Main:**

```auto
import javax.swing.filechooser.FileSystemView;
import javax.swing.JFileChooser;
Menu_bar mp;

void setup() {
  size(600, 600);
  buildMenuBar();
}

void draw() {
  background(0);
  fill(255);
  ellipse(mouseX, mouseY, 30, 30);
}

void buildMenuBar() {
  mp = new Menu_bar(this, getClass().getSimpleName(), width, height);
}

```

**Menu\_bar:**

```auto
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class Menu_bar implements ActionListener {
  JFrame frame;

  public Menu_bar(PApplet app, String name, int width, int height) {
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    frame = (JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas)app.getSurface().getNative()).getFrame();
    frame.setTitle(name);

    // Creates a menubar for a JFrame
    JMenuBar menu_bar = new JMenuBar();
    // Add the menubar to the frame
    frame.setJMenuBar(menu_bar);
    // Define and add two drop down menu to the menubar
    JMenu import_menu = new JMenu("File");
    JMenu text_menu = new JMenu("Edit");
    JMenu shape_menu = new JMenu("Sketch");
    JMenu image_menu = new JMenu("Tools");
    JMenu video_menu = new JMenu("Help");

    menu_bar.add(import_menu);
    menu_bar.add(text_menu);
    menu_bar.add(shape_menu);
    menu_bar.add(image_menu);
    menu_bar.add(video_menu);

    // Create and add simple menu item to one of the drop down menu
    JMenuItem new_file = new JMenuItem("Save");
    JMenuItem new_folder = new JMenuItem("Save As...");
    JMenuItem action_exit = new JMenuItem("Exit");

    new_file.addActionListener(this);
    new_folder.addActionListener(this);
    action_exit.addActionListener(this);

    import_menu.add(new_file);
    import_menu.add(new_folder);
    import_menu.addSeparator();
    import_menu.add(action_exit);

    frame.setVisible(true);
  }
  @Override
    public void actionPerformed(ActionEvent e) {
    JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
    jfc.setDialogTitle("Choose destination.");
    jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

    String ae = e.getActionCommand();

    if (ae.equals("Save")) {
      println("Save");
    } else if (ae.equals("Save As...")) {
      println("Save As...");
    } else if (ae.equals("Exit")) {
      println("Exit");
      System.exit(0);
    }
  }
}

```

---

_[View the full topic](https://discourse.processing.org/t/is-there-a-way-to-add-jmenubar-file-edit-to-my-program/31830)._
