Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JmenuItem

 
samira samadi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add action listener instead of mouse listener.
Also, in future, while posting code, please UseCodeTags
 
samira samadi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mouseClicked won't work, but mousePressed works OK


 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sriraman Srinivasan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic