Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Enabling a disabled Menu Item trigger by registration code

 
Elena Pitic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I know this topic was discussed here, but I would like, still to share my code and problem with you.
Structure of the project:
- MenuPage.java is a JFrame that contains menu bar with menu items that initial have partial active items.
- HelpInregistrare is a JFrame that contains the elements for writing activation code. Once activation code is written, all the items from Menu ( from the page MenuPage.java) should activate except Help - Inregistrare

Here is my code:


Missing code is generated automatically from NetBeans. ( is gray code and cannot be modified so I skipt it)
Code compiles fine, the logic mechanism for " Enabling a disabled Menu Item trigger by registration code " is not written.
I tried several solutions. The solution I got stuck in my head with is:
- I have a check box in the registration form, and is enabled only when registration code is valid.
- in MenuPage. java I tried to obtain if checkbox is enabled or not calling isSelected method.

Conclusion: if checked box is selected some items from Menu are enabled, if is not selected then other items are selected.
It compiles fine, but no visible change, the reason I think is that this enable/disable action happens for a second.. and not permanent.

Should I use threads.. or what solutions you believe can be approached ?
Thank you very much for your time
Elena
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All I can say is, to enable or disable a menu item you call its setEnabled() method. But it looks like you know that already, so I don't understand why you need to consider several solutions. As far as I can see, your actual question is about some code which you haven't posted. So hopefully you can understand why nobody has been able to comment on that code.
 
Elena Pitic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, you are correct.

Here is the checkbox idea


Program starts fine, with the menu inactive. But when activation pass is written, menu is still inactive.
Maybe I should use another event ?

Thank you very much!
 
Campbell Ritchie
Marshal
Posts: 52664
122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Please explain how you are validating and verifying registration codes; that may help us work out how to enable your button.
 
Elena Pitic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you actually it is my first time when I am searching for help this way.. so maybe that is why some information is always missing So from the research I've done.. I notice that usually this mechanism of code registration is usually done via a database (sql) but my version is not.

So I have a known registration code which is : admin.
/*HelpInregistrare.java class */
1. I have JPasswordField called JPcodinregistrare
2. I am extracting information from JPasswordField like this: String cod = String.valueOf(JPcodinregistrare.getPassword());
3. Then I am verifying if what is written in JPasswordField is "admin"like this: cod.contains ("admin")
4. It it is all good.. and what is written by user in JPasswordField is "admin" then this JCheckBox called check is being enabled.
This JCheckBox I put there as a variable in order to tell me if the validation was a success.
Otherwise everyhting is false and JOptionPane comes with Error msg, and JCheckBox stays false and invisible.

/*in MenuPage.java */
this method public void initialize(){ .. } contains the elements that appear when frame is called to be visible.
I call this method in main like this:
MenuPage mp = new MenuPage();
mp.initialize();

So in my mind I am thinking like this: when method initialize() is called from main, there in initialize should be a condition to choose how the menu element should be displayed ( some menu items enable, others not enable)
As a trigger variable I used the JCheckBox : if JCheckBox ( var name check) is true then means registration code is "admin" so the Menu should have those Menu Items enabled.. otherwise.. the other version.

I hope I succeed to explain myself better this time


 
Campbell Ritchie
Marshal
Posts: 52664
122
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Elena Pitic wrote:. . . 2. I am extracting information from JPasswordField like this: String cod = String.valueOf(JPcodinregistrare.getPassword());. . .
That is obviously a very simple and low security password; you would usually take the password as a char[], the hash it. You can use Arrays.equals(password, new char[]{'A', 'd', 'm', 'i', 'n'})

If there is equality, simply call the setEnabled(true) method on the menu item.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand correctly, if the checkbox is checked then certain menu items should be enabled and others should be disabled, and vice versa?

If that is the case then I would write a listener which is called when the checkbox is checked or unchecked. The listener would then enable and disable the menu items as per the requirements.

Then your initialization code would only set up the menu items, it wouldn't have to disable any of them. Initializing the checkbox would do that for you.
 
Elena Pitic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie yes I know is a poor password ( is a course project, and professor who will test the code, specifically asked for a poor password :-) )

@Paul Clapham yes, you understood correctly. I will try with the listener version and I will get back with the code ;-)

Thank you
 
Elena Pitic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the if else statement from initialize() in private void checkActionPerformed(java.awt.event.ActionEvent evt) { // checkbox verification }

Remains the same, the initial Menu with disabled MenuItems, as program starts.

MenuPage.java and HelpInregistrare.java are 2 separete frames.. maybe is this the reason why menu items enable/disable is not working ?

I believe now it occurs the variable shadowing .. since elements are called from 2 different frames ?
 
Elena Pitic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the if else statement from initialize() in

Remains the same, the initial Menu with disabled MenuItems, as program starts.

MenuPage.java and HelpInregistrare.java are 2 separete frames.. maybe is this the reason why menu items enable/disable is not working ?

I believe now it occurs the variable shadowing .. since elements are called from 2 different frames ?
 
Elena Pitic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I have an idea: I will replace the Registration Form from JFrame HelpInregitrare with a JDialog that will contain the Registration Form. I will get back to you after I try this solution. Thank you
 
Elena Pitic
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it was about the JDialog. Now is working as I wanted. Do you need/ want me to print the code?
Thank you for your help!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!