• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Variable from main.java to FXML controller using Midi & Scene Builder

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

first post here
Hoping someone might help I apologize in advance for the non code talk i'm self taught and very old.

I have a Midi signal coming from an external program into MainApp the Midi note numbers are then sent to "private void midiNoteClock(int i)(main line 47) " which then sends to my FXML Class EventHandlerController / void donkey(int i) this bit works well and i can console print the midi numbers . So i now have an Integer or String that i wish to use to do things with, but every thing I have tried doesn't create an error but then again doesn't work. For instance I might want to display the integer in a TextArea (controller line 26) or use it to increase the radius of a circle as (controller line 27) But it doesn't

The other thing i don't quiet understand is if i wanted to send a int from the EventHandlerController to the MainApp i defined final MainApp t3 = new MainApp();(controller line 13) outside of lets say a button and then use a instance inside a button. (Please look at "private void HBA9()" ) (controller line 52). But if i'm sending the other way as in "private void midiNoteClock" to " void donkey(int i)" (main line 50) where can I define a new EventHandlerController() so its outside and does execute all of the EventHandlerController ..... it doesn't let me do this without an error.

If you got this far thanks for reading; This code form part of a larger file .
I've cut it down as far as I can Here is the code: MainApp

here is the controller class

Posts: 6126
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, welcome to the Ranch.

Thanks for the well-formatted code. I'm not a JavaFX expert so I added this thread to that forum. Hopefully people there will have an answer.

Until then, my thought is to pass outputTextArea into donkey() and work with it that way.
Peter Oldman
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Knute for your kind welcome.
Here's hoping someone might point me in the right direction.
Ranch Hand
Posts: 374
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not create a new MainApp inside your controller.
You already have an application instance created for you when the JavaFX application is launched, you should reuse that (or better yet, make the controller self-sufficient and independent of the application using it).
You should not create a new EventHandlerController.
You already have a controller created for you when you load an FXML file.

I'd advise moving all of the midi related stuff out of the Application.
Try to make you classes follow the Single Resposibility Principle https://en.wikipedia.org/wiki/Single_responsibility_principle of SOLID https://en.wikipedia.org/wiki/SOLID_(object-oriented_design).
The application should just implement only the Application interface of co-ordinating initializing, starting and stopping of the application and load up and display of the initial scene.
Also it is weird that your application would both implement Receiver and have a Receiver member variable.
Instead what you should do is use more of an MVC style design, where the application creates the model and the loads up the FXML view (during the process of which the FXMLLoader implicitly creates the controller and links it to the view). What the FXMLLoader won't do is wire in your model objects, to do that, you can review various options in the answer to this question: http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml. MVC is more of a guideline than a strict rule, without knowing your domain it is a little difficult to know if the receivers should be disconnected from your controllers and part of the model or not. For instance you could have your controller implement the receiver interface if the receiver instance should be tightly coupled to an instance of a UI represented by a controller instance. If you need a more flexible design where the receiver is a seperate object which multiple controllers need to interact with, then you could shift it to be part of model which is inserted into controls either via dependency injection (e.g. gluon ignite http://gluonhq.com/open-source/ignite/) or by calling a method on the controller:

You should understand concurrency in JavaFX https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm
You should not perform I/O on the JavaFX application thread, instead you should use a Task https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html for that.
You should not have another thread such as a callback from a midi system modify the JavaFX scene graph, instead you should use Platform.runLater for that.

Don't catch exceptions and then log them without a stack trace otherwise you lose useful debugging info, e.g. instead of
Also, it is extremely rare that you would want to swallow an error without at least reporting it, e.g. don't write:
Things will just go wrong and you will never know.

It looks like you call donkey explicitly. That being the case, you don't need to (and should not) annotate it with @FXML. The @FXML won't actually do anything in such a case, it will just confuse somebody reading the program. @FXML is supposed to be a signal to the FXML loader that the a member variable should be injected into an FXML controller or a method should be wired to service an event handler referenced in FXML, neither of which seems to be the case with donkey.

There may be other issues with your code, they were just some things which came to mind.
//end of the world// nice comment :-)

It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!