We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
drop-down menu? (Read 2857 times)
drop-down menu?
Jun 24th, 2008, 3:39pm
 
This is probably a stupid question but how can you get a simple drop-down menu (with say 5 choices) in Processing?

I'm beginning to learn how to program so my apologies for such a basic question.

Thanks in advance!

-a
Re: drop-down menu?
Reply #1 - Jun 24th, 2008, 7:14pm
 
Its not as simple as you might have thought, this is what I worked out;

Quote:


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

}

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




Its very clunky, and there's many more things you can do with it. You're going to want to look at the java docs for the frame, MenuBar, Menu, MenuItem classes to learn more;

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Frame.html

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/MenuBar.html

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Menu.html

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/MenuItem.html
Re: drop-down menu?
Reply #2 - Jun 24th, 2008, 7:16pm
 
amazing.

not simple at all. Thanks so much!

a

Re: drop-down menu?
Reply #3 - Jun 25th, 2008, 11:49am
 
or maybe you'd like to use a GUI library, like controlP5
http://www.sojamo.de/libraries/controlP5/

see Reference > Librairies > Graphic interface
Re: drop-down menu?
Reply #4 - Jun 25th, 2008, 11:10pm
 
antiplastik wrote on Jun 25th, 2008, 11:49am:
or maybe you'd like to use a GUI library, like controlP5
http://www.sojamo.de/libraries/controlP5/

see Reference > Librairies > Graphic interface



This is very promising but I can't figure out what folder their libraries folder needs to go into. On a Mac, does the libraries folder get put into the root directory that all the files are in ("Processing" folder) or does it go into the sketches folder (folder for sketch I'm working on)

thanks for replying. Every little bit helps as I learn to "conquer" Processing and scripting in general.
Re: drop-down menu?
Reply #5 - Jul 9th, 2008, 5:23pm
 
Is it possible to erase or overwrite the 'processing.core.PApplet' menu item?
(or in the case of exporting it as an application, erase the name of the program as a menu item)

thanks!!
Re: drop-down menu?
Reply #6 - Jul 9th, 2008, 7:02pm
 
@Manic

Thanks for the example.  I had one question, where would you declare "frame", and then how does the frame relate to your PApplet?
Re: drop-down menu?
Reply #7 - Jul 20th, 2008, 7:47pm
 
Is it possible to erase or overwrite the 'processing.core.PApplet' menu item?  
(or in the case of exporting it as an application, erase the name of the program as a menu item)

ANY IDEAS ANYBODY??

thanks!

Re: drop-down menu?
Reply #8 - Jul 20th, 2008, 10:55pm
 
Michael, even after you repeated your question, I still understand it.
I don't see anything about "processing.core.PApplet" in the given code fragment, and after I tried it, I still don't see it.
So what are you talking about?
Re: drop-down menu?
Reply #9 - Jul 21st, 2008, 1:06pm
 
Hello PhiLho,

maybe this is macintosh specific?
when i run the code above, i get the 'colours' menu to work, but i get an additional boldface  'processing.core.PApplet' menu to the left of it.
i am trying to make this disappear.

when i export the program as an application,  the 'processing.core.PApplet' menu is replaced by a menu giving the program's name.
it would be great to get this to disappear as well.

any ideas?  perhaps this is only the case on mac?

thanks!!

Re: drop-down menu?
Reply #10 - Jul 21st, 2008, 9:43pm
 
On OS X, the application name is always displayed with a bare bones menu attached.  Once you export to application you can edit this name by right clicking on the OS X app and going to "Show Package Contents," then opening up Info.plist.  You need to alter the CFBundleName parameter.

If you REALLY want to hide it, you can sort of achieve this by setting CFBundleName to " " (that's a single space) - that way the menu will still be there, but you won't see it.  That's going against OS X conventions, though, so I don't really suggest it.

If you're interested in further OS X customizations, you might take a look at http://java.sun.com/developer/technicalArticles/JavaLP/JavaToMac/, http://java.sun.com/developer/technicalArticles/JavaLP/JavaToMac2/, and http://java.sun.com/developer/technicalArticles/JavaLP/JavaToMac3/, all of which have some good tips.  VERY non-cross platform stuff, I warn you (Apple did to Java on OS X what Microsoft would have loved to do to it (and tried to, but got smacked down) on Windows, which is add a bunch of non-standard crap that breaks write-once-run-anywhere).
Re: drop-down menu?
Reply #11 - Jul 22nd, 2008, 10:06am
 
Thank you very much! fantastic...
Page Index Toggle Pages: 1