Hello people of the internet. I'm in a situation where I need to create a GUI to create objects of various classes and I want to do so in a generic way. To be more specific, the whole concept is to create various products for a shop. For this example, product categories will be Notebooks and Pens. After selecting a product category from a list, a form JFrame window will appear where the user will be prompted to give more specifics about the product.
However, not all products require the same data. Let's assume we have a Notebook class requiring the page number as Integer, and a Pen class requiring the pen color as String. Obviously, we need to handle the creation of this window differently for each product type.
So my question is, where should I handle the creation of this window? Is it ok to create a method in each data class which creates a complete JFrame of the form required, or should I handle gui seperately from the data classes?
Thanks in advance!
You would not want to have GUIs interwoven with those classes. It is probably a good idea to look at MVC. Model-View-Controller.
The idea is that your GUI is seperated from your Model, with a Controller inbetween of them which deals with moving data from and to your GUI. There are two common variants of this, which is to either let the Model notify the View that it has new data which is available, or that the Model pings the Controller again and that the controller then notifies the View of this new data.
This is an architecture pattern. They provide some solutions to commonly occuring problems in Software Engineering, and it's a good idea to read up on them :-)
Use MVC (model view controller) which is an Architectural pattern that is used to separate the data from the user interface, the data (methods you need etc..) should be put in the model, the view handles your interface components(panels , frames etc...), and the controller relates inputs from the view to your data in the model.
In your case if you have a notebook object you should notify the controller which then notifies the user interface that displays the required form for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With