Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes JmenuItem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JmenuItem" Watch "JmenuItem" New topic
Author

JmenuItem

samira samadi
Greenhorn

Joined: Jul 06, 2010
Posts: 9
hi
I'm working with netbeans GUI and I have a menu named:file
I want this to have sub menus like:new,load,...
It seems that I should use jMenuItem,but I can not add events to it(new for example) like usual,because I added mouseClicked event,but when I click :new nothing happens.
I would be so thankful if you help me.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
follow the tutorial/examples here

http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html

and if you have problems with those, post what you've tried
samira samadi
Greenhorn

Joined: Jul 06, 2010
Posts: 9
It says that handling events for jMenuItem is just like Jbuttons,but I do the sane and it does not work!
here is some parts of my code related:

private javax.swing.JMenu FilejMenu;
FilejMenu = new javax.swing.JMenu();

private javax.swing.JMenuItem NewjMenuItem;
NewjMenuItem = new javax.swing.JMenuItem();

FilejMenu.setBackground(new java.awt.Color(102, 102, 102));
FilejMenu.setText("File");
FilejMenu.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
FilejMenuMouseClicked(evt);
}
});

NewjMenuItem.setAction(NewjMenuItem.getAction());
NewjMenuItem.setText("New");
NewjMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
NewjMenuItemMouseClicked(evt);
}
});
FilejMenu.add(NewjMenuItem);


private void NewjMenuItemMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
System.out.println("bbb");
}
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

You need to add action listener instead of mouse listener.
Also, in future, while posting code, please UseCodeTags


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
samira samadi
Greenhorn

Joined: Jul 06, 2010
Posts: 9
thank you very much.
it worked,but would you tell me why?
I thought ActionListener is a general form and mouseListener for example th espesial one...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
mouseClicked won't work, but mousePressed works OK


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

samira samadi wrote:I thought ActionListener is a general form and mouseListener for example th espesial one...

Definitely not. They are for two very different types of events. MouseListener is for mouse interaction in general, whereas ActionListener is for button clicks, menu item clicks, combo box changes, text field enter presses, and a whole lot more.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sriraman Srinivasan
Greenhorn

Joined: Jul 08, 2010
Posts: 10
Just like Rob had mentioned both have different purposes. Action listener is basically for button press action. JMenuItem being a subclass of Abstract button, you can consider it as a special type of button itself. So practically all actions which can take place in button will also occur in menu item.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Sriraman Srinivasan wrote:Action listener is basically for button press action.

That's the most often use of it, but definitely not the only. Like I said, pressing enter on a JTextField or selecting an element from a JComboBox also triggers ActionEvents. javax.swing.Timer also uses it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JmenuItem
 
Similar Threads
Adding Submenu to JMenuItem.
JMenuItem.setMargin
setMnemonic(int)
menu not showing up on gui window
Help Please Urgent