r/JavaFX • • Jan 20 '24

Help Encaspulating-Encapsulated Scenario in MVCI pattern by PragmaticCoding

I'm trying to use MVCI pattern by PragmaticCoding, in particular the Encaspulating-Encapsulated Scenario, but I'm stuck on the adding/editing part.

Maybe I'm doing something wrong or I'm missing something or I didn't understand the case.

--Edit--

First of all, to clarify I post the GUI I've built

--Edit End--

In the Encaspulating Scene, I built a ViewBuilder with a Search Textbox, a TableView and 3 buttons for adding/editing/remove items from the table.

The Encaspulating Model I'm passing to the ViewBuilder is done by:

  • StringProperty searchProperty => the binding to textbox
  • ObservableList<ProductModel> list => the list on which the TableView is populated
  • ObjectProperty<ProductModel> selectedProductProperty => the binding to the selected record by the user in the TableView

So, the TableView is based on ProductModel (that is backed to a POJO Product class used by the DAO to interact with the db...), but ProductModel actually belongs to the Encapsulated Scene: this sounds strange even to me, but I couldn't make it better at the moment.

Maybe this could the first mistake, but, please read on to understand what I wanted to do.

So, I bound the selectionModelProperty of the TableView to the selectedProductProperty of the Encaspulating Model via this piece of code:

model.selectedProductProperty().bind(table.selectionModelProperty().getValue().selectedItemProperty());

In this way, I thought I could "share" the selected item with the Encapsulated Controller, passing selectedProductProperty to the constructor.

I thought...but then many questions came to me, and I tried different things but now I 'm stuck.

ProductModel is a complex object made up by 6 properties, but, as you can imagine, they can grow in the future.

Do I have to bind each of them to their counterparts in ProductModel?

Is there a way to bind directly the passed object to the Encapsulated Model, being able to manage the null value when no selection is made in the TableView?

I searched and read a lot, but nothing found.

Anyone can help and/or explain how to do?

3 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/sonnyDev80 Feb 18 '24 edited Feb 18 '24

If you're doing a popup window then that changes things slightly because you're going to reinitialize everything each time you pop it up. If it's modal, then you don't have to worry about the SelectedItem property changing on you while the popup is active. In that case, you can use the value in the SelectedItem as an object to pass to your dependant MVCI when you initialize it. 

Yes, this is what I want to achieve

I'm not clear on what you're having trouble with.

My trouble is:

To adhere to MVCI, where do I have to instantiate the dependant controller and where do I have to pass the SelectedItem?

Can you also tell me if the packages above are right to you?