# Show an desktop menu bar

**URL:** https://discourse.processing.org/t/show-an-desktop-menu-bar/9651
**Category:** Coding Questions
**Created:** [March 26, 2019, 4:19pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651 "2019-03-26T16:19:08Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)
#### Post date: [March 26, 2019, 4:19pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/1 "2019-03-26T16:19:08Z")

</div>

I try to add a menu bar on the top of my desktop, i’m close but I fail :slight\_smile  
Obviously I don’t want open a new frame, but I cannot figure the way to do that.

my code is here

```auto
Menu_bar mp;

void setup() {
	buildMenuBar();
}

void draw() {
}

void buildMenuBar() {
	mp = new Menu_bar("Media", 100,100);
	mp.exec();
}

```

```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 extends JFrame {
	String name;
	int width;
	int height;

	public Menu_bar(String name, int width, int height) {
		System.setProperty("apple.laf.useScreenMenuBar", "true");
		this.name = name;
		this.width = width;
		this.height = height;
		setTitle(name);
		setSize(this.width,this.height);
		
		// Creates a menubar for a JFrame
		JMenuBar menu_bar = new JMenuBar();
		// Add the menubar to the frame
		setJMenuBar(menu_bar);
		// Define and add two drop down menu to the menubar
		JMenu import_menu = new JMenu("import");
		JMenu text_menu = new JMenu("text");
		JMenu shape_menu = new JMenu("shape");
		JMenu image_menu = new JMenu("image");
		JMenu video_menu = new JMenu("video");

		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("Import file");
		JMenuItem new_folder = new JMenuItem("Import folder");
		JMenuItem action_exit = new JMenuItem("Exit");

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

		// Add a listener to the New menu item. actionPerformed() method will
		// invoked, if user triggred this menu item
		new_file.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("You have clicked on the new action");
			}
		});
	}

	public void exec() {
		Menu_bar me = new Menu_bar(this.name, this.width, this.height);
		me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		me.setVisible(true);
	}  
}

```

![46](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6b060fc174ac287cf8ae05d2f93706c28bfa1397.jpeg)

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [March 26, 2019, 4:49pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/2 "2019-03-26T16:49:40Z")

</div>

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

```auto
Menu_bar mp;

void setup() {
  buildMenuBar();
}

void draw() {
}

void buildMenuBar() {
  mp = new Menu_bar(this, "Media", 100, 100);
}

```

Menubar tab

```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 {
  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("import");
    JMenu text_menu = new JMenu("text");
    JMenu shape_menu = new JMenu("shape");
    JMenu image_menu = new JMenu("image");
    JMenu video_menu = new JMenu("video");

    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("Import file");
    JMenuItem new_folder = new JMenuItem("Import folder");
    JMenuItem action_exit = new JMenuItem("Exit");

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

    // Add a listener to the New menu item. actionPerformed() method will
    // invoked, if user triggred this menu item
    new_file.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        System.out.println("You have clicked on the new action");
      }
    }
    );
    frame.setVisible(true);
  }

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [March 26, 2019, 5:06pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/3 "2019-03-26T17:06:44Z")

</div>

thanks, but here ( Raspberry Pi ) i must use

```auto
// frame = (JFrame) (processing.awt.PSurfaceAWT.SmoothCanvas)app.getSurface().getNative()).getFrame();
    frame = (JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas)app.getSurface().getNative()).getFrame();

```

and in setup to see something, like

```auto
void setup() {
  size(500,500);
  buildMenuBar();
}

```

![2019-03-27_00-04-50_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/459c777caa85f8c56b8a0e1fc11fc9d0bd65f0fa.png)

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [March 26, 2019, 5:13pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/4 "2019-03-26T17:13:25Z")

</div>

> [@kll](#):
>
> thanks, but here ( Raspberry Pi ) i must use

Sorry accidentally deleted a ‘(’ I have corrected the code above 😉

You can always put the statement

```auto
frame.setVisible(true);

```

as the last line of the constructor, if fact it makes sense. I will modify the code above to do that.

---

<div class="post-metadata">

### Author: ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)
#### Post date: [March 26, 2019, 5:26pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/5 "2019-03-26T17:26:00Z")

</div>

Thx @quark that’s great because in my case I don’t need `P2D` or `P3D` but if I need one day to do that with an `OPENGL`rendering is not possible if I understand well ?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 26, 2019, 5:34pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/6 "2019-03-26T17:34:21Z")

</div>

It should be possible. But you’re gonna need to grab a diff. datatype which would correspond to the JAVA2D’s own JFrame:

> [@Event called when canvas is resized](https://discourse.processing.org/t/event-called-when-canvas-is-resized/6643/8):
>
> Hi @Sarah! Here’s a sketch I’ve been working w/ many months ago. construction_worker_man It’s about creating a class callback which triggers every time the canvas resizes. robot It’s still experimental. man_scientist Yet to finish it 100% . But it’s runnable already: running_man

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [March 26, 2019, 5:45pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/7 "2019-03-26T17:45:27Z")

</div>

JFrame and JMenubar are part of the Java Swing frame work and is incompatible with OpenGL and GLWindow, so it won’t be possible to do it this way. As GoToLoop pointed out you would need to find the OpenGL equivalent of menubars, but as far as I know there isn’t one.

There is an alternative to using P2D and P3D, and that is to use JAVA2D for the main sketch and then use OpenGL off-screen buffers for drawing which are then copied to the main sketch display in the draw() method. G4P uses this in its latest version for the GView control.

---

<div class="post-metadata">

### Author: ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)
#### Post date: [March 26, 2019, 5:52pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/8 "2019-03-26T17:52:34Z")

</div>

Thx for the advice @GoToLoop and @quark I think stay on classic renderer. But if I need I know now there is a trick !!!

---

<div class="post-metadata">

### Author: ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)
#### Post date: [March 27, 2019, 11:17am UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/9 "2019-03-27T11:17:49Z")

</div>

Now the problem very very important is catching the information and I don’t any possibility to do that.

How catch something in this part to use in other place of my code. I try to pass argument, but the arg need to be `static` ☹  
And I don’t find on the internet a method to catch something the `JMenuItem` like boolean statement .  
I figure the probleme is around this part:

```auto
public void actionPerformed(ActionEvent arg0) {
   println("happen when is clicked");
}

```

Refactoring code:

```auto
Crope_Bar cb;
void setup() {
	cb = new Crope_Bar(this);
	cb.help();
	cb.set("about","file,load,load recent,|,save,save as","import,file,folder","help,controler,prescene,scene");
	cb.show();
}

void draw() {
}

```

```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 Crope_Bar {
	JFrame frame;
	JMenuBar menu_bar;

	public Crope_Bar(PApplet app) {
		System.setProperty("apple.laf.useScreenMenuBar", "true");
		frame = (JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas)app.getSurface().getNative()).getFrame();
		menu_bar = new JMenuBar();
		frame.setJMenuBar(menu_bar);
	}

  ArrayList<JMenuItem> menu_item_list = new ArrayList<JMenuItem>();
	void set(String... data) {
		menu_item_list.clear();
		build(data);
		set_listen();
	}

	void build(String... data) {
		JMenu [] menu = new JMenu [data.length];
		for(int i = 0 ; i < data.length ; i++) {
			println(data[i]);
			String[] content = split(data[i],",");
			menu[i] = new JMenu(content[0]);
			menu_bar.add(menu[i]);
			if(content.length > 1) {
				for(int k = 1 ; k < content.length ; k++) {
					if(content[k].equals("|")) {
						menu[i].addSeparator();
					} else {
						JMenuItem menu_item = new JMenuItem(content[k]);
						menu_item_list.add(menu_item);
						menu[i].add(menu_item);
					}
				}
			}
		}
	}
  
	void set_listen() {
		if(menu_item_list.size() > 0) {
			for(JMenuItem mi : menu_item_list) {
				mi.addActionListener(new ActionListener() { 
					public void actionPerformed(ActionEvent arg0) { 
						println("You have clicked on menu but how use it out this line ?????, because I just can pass a static variable here :(");
					}
		    });
			}
		}
	}

	void show() {
		frame.setVisible(true);
	}
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 27, 2019, 12:10pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/10 "2019-03-27T12:10:51Z")

</div>

I believe it’d be much clear for us to figure out your code if you’d post it fully on some GitHub repo. 🤔

---

<div class="post-metadata">

### Author: ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)
#### Post date: [March 27, 2019, 12:39pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/11 "2019-03-27T12:39:17Z")

</div>

et voilà ! [https://github.com/StanLepunK/Crope](https://github.com/StanLepunK/Crope)  
you just need to dowload my little Rope library to performe the sketch.  
the problem is on this tab: [https://github.com/StanLepunK/Crope/blob/master/crope\_gui/menu\_bar\_default\_render.pde](https://github.com/StanLepunK/Crope/blob/master/crope_gui/menu_bar_default_render.pde)

---

<div class="post-metadata">

### Author: ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)
#### Post date: [March 28, 2019, 3:45pm UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/12 "2019-03-28T15:45:58Z")

</div>

Finally i need to make a bridge from `P3D` / `P2D` to `JFrame` but you example is very hard to understand… after I catch that : `com.jogamp.newt.opengl.GLWindow glw = (com.jogamp.newt.opengl.GLWindow)app.getSurface().getNative();`what the next step ?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 29, 2019, 12:04am UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/13 "2019-03-29T00:04:19Z")

</div>

> [@Stanlepunk](#):
>
> What’s the next step?

On that [**CanvasResizeWatcher.java**](https://gist.github.com/GoToLoop/1d1eb18f468a7d5758469707276b6ea1#file-canvasresizewatcher-java), the only thing it uses **getNative()** for, regardless of the renderer, is to add & remove listeners to the sketch’s canvas. 🖼

I’m afraid if you need to do more stuff to it, you’re gonna need to study the JOGL API: 😧  
[JogAmp.org/deployment/jogamp-next/javadoc/jogl/javadoc/](http://JogAmp.org/deployment/jogamp-next/javadoc/jogl/javadoc/)

BtW, many of your code’ classes can be easily converted from “.pde” to “.java”. ☕

---

<div class="post-metadata">

### Author: ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)
#### Post date: [March 31, 2019, 8:52am UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/14 "2019-03-31T08:52:09Z")

</div>

> [@GoToLoop](#):
>
> BtW, many of your code’ classes can be easily converted from “.pde” to “.java”. ☕

Which part of my code you talk to converter in Java ? If I remenber the Java code need to put in folder `code`?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 31, 2019, 10:23am UTC](https://discourse.processing.org/t/show-an-desktop-menu-bar/9651/15 "2019-03-31T10:23:18Z")

</div>

Any file w/ a class which accepts a PApplet can easily be renamed & turned into a “.java” file. ☕
