Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Grab menu items from a menu controller within a different controller

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a student for Capella University and I'm taking a course that has thrown me for a loop in learning JavaFX. What my professor is wanting, only certain menu items are disabled then upon successful login the menu items are enabled. I have figured out how to disable all menus until successful login but that isn't what he wants and he will surely count off. Here is what I have below.

EDIT: Oh yeah, keep in mind that I have to do this without changing any of the current code, I can only add to the code.


As you can see within the createEmployeeAccountScene, I have added final MenuBar addMenu line that does what I need to but I am trying to accomplish the following:


The above code is just an example as I know that code won't work but that is what I'm trying to accomplish then pass that variable to employeeSignIn and enable those items upon successful login.

Thanks to anyone that is able to help
 
Sheriff
Posts: 16660
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

By passing in the hard-coded value of true, you're saying that menu item will be disabled all the time. Instead of hard-coding the value, then pass in a variable that's set to true when the user isn't logged in, and set to false when the user is logged in.
 
Steven Moore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried that and my professor did not like that so I had to remove it. I got this method to work how I wanted, I just need a way to grab each menu item so I can disable only the ones I want.
 
Junilu Lacar
Sheriff
Posts: 16660
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show us what you tried that your professor didn't like. When asking for help about code, it's best to show the code so people don't make any assumptions.
 
Junilu Lacar
Sheriff
Posts: 16660
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example of someone (me) making assumptions based on unseen code:

Maybe your professor was expecting separate methods to enable/disable just the menu items that were supposed to be "behind" the login page. That is, maybe the expectation was one or two methods:
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I may be misunderstanding, but can't you do that through the menuXX variables?
 
Steven Moore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Here's an example of someone (me) making assumptions based on unseen code:

Maybe your professor was expecting separate methods to enable/disable just the menu items that were supposed to be "behind" the login page. That is, maybe the expectation was one or two methods:


That would be fine and dandy if I knew how to pull in the menus then disable only the menu items I wanted.

Knute Snortum wrote:Well, I may be misunderstanding, but can't you do that through the menuXX variables?


Yea, tried that route and it required me to set a variable of isDisabled and required me to use that variable throughout every scene which is what my professor doesn't want.
 
Junilu Lacar
Sheriff
Posts: 16660
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Moore wrote:
That would be fine and dandy if I knew how to pull in the menus then disable only the menu items I wanted.


Fine and dandy, indeed. The Menu has a getItems() method that returns an ObservableList<MenuItem> which means you can iterate over it and do something with each item. That "do something" might be, oh I don't know, check if its text (MenuItem.getText()) is in a list of menu item texts that should be disabled/enabled.

If you figure out how to iterate over the list, it's not much more effort to figure out how to change attributes of particular items in the list.
 
Steven Moore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Steven Moore wrote:
That would be fine and dandy if I knew how to pull in the menus then disable only the menu items I wanted.


Fine and dandy, indeed. The Menu has a getItems() method that returns an ObservableList<MenuItem> which means you can iterate over it and do something with each item. That "do something" might be, oh I don't know, check if its text (MenuItem.getText()) is in a list of menu item texts that should be disabled/enabled.

If you figure out how to iterate over the list, it's not much more effort to figure out how to change attributes of particular items in the list.



Thanks. As I said, I'm new to this whole JavaFX so I'm always looking for example code which is how I learn to code better and faster. Below is an update to what I have figured out what to do so far.



The only thing I don't know how to code is calling disableMenuItems(). Once I can do that, I can pass that to the employeeSignIn so it calls enableMenuItems(). I can't put it within the button setOnAction otherwise it will enable them when the button is pressed.
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Moore wrote:The only thing I don't know how to code is calling disableMenuItems(). Once I can do that, I can pass that to the employeeSignIn so it calls enableMenuItems(). I can't put it within the button setOnAction otherwise it will enable them when the button is pressed.



It seems to me that you want to create the menu items, then immediately disable them. That shouldn't be hard. Or am I missing something?
 
Steven Moore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Steven Moore wrote:The only thing I don't know how to code is calling disableMenuItems(). Once I can do that, I can pass that to the employeeSignIn so it calls enableMenuItems(). I can't put it within the button setOnAction otherwise it will enable them when the button is pressed.



It seems to me that you want to create the menu items, then immediately disable them. That shouldn't be hard. Or am I missing something?



Yep, your missing something. I have to call the menu items in another method, disable them there, then enable them upon successful login.
 
Paul Clapham
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see what I've missed. The fact that you have to use a different method is just a triviality, as far as I can see. Why don't you write some code that attempts to do that and show me why it isn't actually possible?
 
Steven Moore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I don't see what I've missed. The fact that you have to use a different method is just a triviality, as far as I can see. Why don't you write some code that attempts to do that and show me why it isn't actually possible?



I said this before, it's not up to me, my professor refuses the code and doesn't explain anything. He is the worst professor, he doesn't teach us anything. We were thrown into JavaFX with no guidance let alone any coding help.
 
Paul Clapham
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not impressed by teachers who impose arbitrary restrictions which aren't really in line with good programming practice in order to teach some other thing. But look. In Java when you want to do two things one after the other, you put the code for those two things in the code one after the other, right? That's a rule in every language I've ever worked with.

So I suggested you want to create the menu items and then disable them. That means you put those two pieces of code to run consecutively. So far you have:



which creates the menu items. (Yeah, I know there's more but I don't want to belabour the point.) Now after that you want to put the code which disables them. You've already got code which does that, I've seen it earlier in the thread. So in real life you'd just put it right there, right after you initialize the menu items. But some PHB tells you that you have to put the menu-item-disabling code in a separate method. Fine. Maybe the idea is you might need to re-disable the menu items at a later time, maybe in response to a logout button. So instead of putting the menu-item-disabling code right there after the menu-item-creating code, you put it elsewhere in a method. And then you call that method right after the menu-item-creating code.

Maybe there's something I'm missing in that description, but I still think you should start with that. Give it a try and let us know if you run into problems.
 
Steven Moore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I'm not impressed by teachers who impose arbitrary restrictions which aren't really in line with good programming practice in order to teach some other thing. But look. In Java when you want to do two things one after the other, you put the code for those two things in the code one after the other, right? That's a rule in every language I've ever worked with.

So I suggested you want to create the menu items and then disable them. That means you put those two pieces of code to run consecutively. So far you have:



which creates the menu items. (Yeah, I know there's more but I don't want to belabour the point.) Now after that you want to put the code which disables them. You've already got code which does that, I've seen it earlier in the thread. So in real life you'd just put it right there, right after you initialize the menu items. But some PHB tells you that you have to put the menu-item-disabling code in a separate method. Fine. Maybe the idea is you might need to re-disable the menu items at a later time, maybe in response to a logout button. So instead of putting the menu-item-disabling code right there after the menu-item-creating code, you put it elsewhere in a method. And then you call that method right after the menu-item-creating code.

Maybe there's something I'm missing in that description, but I still think you should start with that. Give it a try and let us know if you run into problems.



That is what I posted earlier where I put the menu item disabling code in its own method and menu item enabling code in its own method. I just don't know how to call those methods within the login scene to disable them then call that method from an entirely new .java file that is my DB and query file. Yes, my professor is a PHB and honestly, I can't wait until this class is over. I hope I don't get him for my next class which is the follow up class to this one.
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's hard for me to understand what you're trying to accomplish, but am I right to say that you want to enable all menu items when the submit button is pressed?
If that's so, look at the clear form code just below it:
There is a subtle difference in the lambda (that's the e -> part of the code) in the clear form code.  You are able to write two (or more) statements if you use a brace ({) after the arrow operator.  How about using that to call your enable menus method in your sing in code?
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A note about your posting: you don't have to (and shouldn't) post all of the text from the post immediately above yours.  Only quote text to provide context and the context is clear if you are posting an immediate reply.  See WhenToQuote (that's a link).
 
Steven Moore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDIT: Sorry about the quoting, just a habit I got into over the many years I have been a website and forum developer.

Sorry, I tried to explain it the best I could. The way I have it set is in CapestraApp.java is my methods and scene, I want to disable certain menu items only for one scene which is the login scene. Once the login button is pressed that leads to CapestraDB.java, upon a successful login verification I want it to enable those menu items that were disabled. I've managed to disable the entire menu using the following code:

I then pass:

To my login process method which then passes it to my query method in CapestraDB.java, once verification is confirmed there I then have it in an if condition as such:

I know it's a lot of code to go through but you can see the code in my posts before this one in full.
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I think I'm understanding the problem.  You want to be able to access references to the menus (so you can disable them) from another class than they were instantiated in.  Passing data from one controller to another is difficult in JavaFX.  I have written a little application that demonstrates one way to do this.

https://github.com/ksnortum/javafx-pass-data-demo

Basically, you create a class to hold all the data (in your case, menus) then you inject a reference to this class into the controller where you want to use it. But I would be surprised if this is something your would do in a beginning JavaFX class.  
 
Steven Moore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I'll check it out once I'm done with this weeks assignment.
 
Steven Moore
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally had a chance to check out the demo you linked, which actually looks a lot like what I needed. Thanks for all the help.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic