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

This is also a possibility, thank you for your efforts :smile:

I just found how to create a menu:

I’ve revised that a bit:

Main:

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:

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);
    }
  }
}

4 Likes