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.
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.
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!
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.
There are no new questions, but there may be new answers.
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.