• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Node organization: when to create a class, when to extend, and when to throw them all in one basket.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, hello everyone! I am a hobbyist programmer that programs mostly for the love of doing it. I recently finished a book, Introduction to Java Programming, Comprehensive Version (10th edition) by Dr. Lang, and am now trying to get a handle of all of the tools in my tool belt. My current project involves a somewhat complicated GUI (to me at least) and I am not sure exactly how to organize all of the nodes for readability and usability. For the sake of clarity, I made a graphic to explain my problem without having to go into the various details of my program.

Suppose that my GUI is comprised of three HBoxes containing three buttons each, all nested in a VBox.

My questions are:
(1) Should each HBox be its own individual class, each of which are initialized in and placed in the VBox?
(2) Should the Boxes extend one another (VBox is the base and each HBox extends the next)?
(3) Should I make one large class that contains all of the nodes?
(4) Or something else?

My concerns with each are:
(1) Accessing the buttons gets pretty verbose. Ex: vBox.hBox1.bt1.setOnAction()..... But the individual classes makes it easier to follow (I think)
(2) May be more difficult to read, but is less verbose
(3) Less verbose, but more difficult to read

Obviously, I am open to any and all help. This includes completely restructuring my GUI. Thank you all so much in advance!
JavaFXNodeOrganization.PNG
[Thumbnail for JavaFXNodeOrganization.PNG]
A visualization of my question
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can implement all your interface in different own classes which will be inherited from original objects. It's so more of working, also you will find the problem with rendering question, but it will be fine. Good Luck!
 
Rancher
Posts: 387
30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to create your own classes at all.
You can just reference the items directly.
You wouldn't make a call like the following:

for numerous reasons:
1. hBox1 isn't a member of VBox.  It would be a child, so, to get it you could do vBox.getChildren().get(0).
2. If you are writing it in code, you normally build the UI from the inner most item up (e.g. create the button, then the hBox, then the VBox).  When you do this you directly have a reference to the button.
3. Because you will have a direct reference to bt1, then you can just reference it directly to call methods on it:
There are also alternatives to creating classes.  An often used one is just to use a factory method:
4. You can lookup items deep in the scene graph using a css selector via node.lookup("selector"); (after applyCSS() and layout() calls have been made).

Now, that is not to say that you will never create your own classes encapsulating controls.  Often that makes sense.  It's just to show you that there are alternatives.  When you do create your own classes it would be for the standard reasons you do so in Object Oriented programming: encapsulation, inheritance, polymorphism, etc.  Of those, encapsulation is by far the most commonly used reason, with inheritance and polymorphism usually being used far less in practice.

The preferred alternative for large apps is usually to use FXML and a controller to represent elements.
 https://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm
 https://docs.oracle.com/javase/8/javafx/fxml-tutorial/
 https://docs.oracle.com/javase/8/javafx/fxml-tutorial/custom_control.htm
 https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html

Note, when you use FXML, the @FXML annotation will directly inject references to all FXML created nodes into the FXML controller, so the controller code can directly reference them.  It can also directly assign event handlers by, for example, setting `onAction="#eventHandlerName"` in the FXML and defining `public void eventHandlerName(ActionEvent event) {}` in the Controller.

For advanced users, if you are writing a general purpose widget to be used in a library, then creating a control with a skin can be an option:
 https://wiki.openjdk.java.net/display/OpenJFX/UI+Controls+Architecture
 
Sheriff
Posts: 7125
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
If you would like to look at a fairly simple JavaFX project that I did, go here.  It's not perfect by any means, but it illustrates one way to organize your project.  You could also search github.com for JavaFX and probably find a lot of projects.
 
Knute Snortum
Sheriff
Posts: 7125
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
Note on your post: we don't have permission to see this image:

https://lh3.googleusercontent.com/deSv2D8t2H4COlQmhgsu28eHWUbSLNf1P3r18ClB7vyDDkhH47YveqxjkTrApHn3UBd_VQn3rrN-lJ4=w1920-h935

You can either change the access rights to that resource or post an image to Google Drive with access rights for everyone.
 
Austin Keeton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John Damien Smith, thank you so much for that explanation. When I posted this question I did not expect such in-depth help. You are awesome! That answered my question perfectly and with great clarity.

Knute Snortum, thank you for the link to your example. One thing I felt like the book that I read lacked was information on the proper way to organize your classes and resources. Your example will definitely help me understand that better in addition to my understanding of JavaFX.

What a fantastic forum
reply
    Bookmark Topic Watch Topic
  • New Topic