Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with JMenu.removeAll()  RSS feed

 
Arnes Arnautovich
Greenhorn
Posts: 9
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code:



The problem is I don't get any menu items whether the user is logged in or not. But if I put these menu items outside of addActionListener function they appear just nice. But I need to have some sort of testing so I need them inside.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The addActionListener() method is for adding Listeners, not for adding menu items. You have actually put the adding menu items in the actionPerformed method, so you should get them added when you click the button. But that is almost certainly not what you wanted.

I don't see how that would be useful for testing. In fact you don't usually want menus which change their items as the application runs.
 
Arnes Arnautovich
Greenhorn
Posts: 9
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you do it then? There needs to be login menu item that opens a frame in which the user enters his credentials and those are then checked within database. I did that and it works. If I enter correct username and password I get a message like Success or something.
Now If user logs in then the Login and Register menu items are no longer needed. So I need to remove them.
I f you have a suggestion please give it. Thanks!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should get them added when you click the button.

No, account is a JMenu, not a button, and that class doesn't fire an ActionEvent when clicked.

Arnes, you can use a MenuListener instead. But I would suggest that the correct approach is to update the menu only when the login status changes. You launch the application with 'Login' and 'Register' menu items, then when a login is successful you remove them and add the 'Logout' menu item. Do the reverse in the ActionListener of the 'Logout' item.

Moving to the GUIs forum.
 
Arnes Arnautovich
Greenhorn
Posts: 9
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Darryl, it worked. The reason I use listener on menus instead only when the status of the user changes is because my login window is in another method so if I want to use those three menu items I have to reference them as arguments of the method and I already am referencing one object that handles users state, sort-of "bellow the hood". Four arguments just looks too much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!