I'm currently building a Java GUI application and I'm about to implement something that seemingly requires a great deal of thinking, at least for someone as relatively inexperienced as me.
The situation
is as follows: I have a class Customer
at domain level, that holds a collection of jobs, which is a class too (Job
). The application is for a fictious gardening company that wants to manage its customers and the jobs it does for them.
At GUI level, I have a jList for all the customers, with a custom listmodel that extends AbstractListModel
. This part of my application works perfectly, I can add/edit/remove customers easily and as it should be.
The goal
now is to implement a jTable that should show the jobs for the customer selected in the jList.
No doubt I'll need a custom TableModel
, the question is: where to get the data from? It should be noted that the collection of jobs in the Customer
class is an ArrayList
, and the class has no method that returns this ArrayList directly, it is only possible to get a copy, since I don't want it to be possible to mutate the collection in the class directly from a public context.
My idea
Is to let the tablemodel have a method to change the internal collection, with a customer
parameter. When the index in the jList
changes, that method should be invoked, so that the jTable represents the Job
s that have been done for the customer.
When I have to edit/create/remove a job, the changes are always done on the tablemodel first, which will propagate the changes to the customer
object (in case of a new job or job removal).
The question
Is that a good way to implement it? I feel it is not, because if I forget to do any changes to a Job
via the tablemodel, and on the Customer
or Job
directly, there will be inconsistency and trouble. Are there better ways? If it involves changing other stuff, I do not mind.
(optional) Small subquestion
I lack some knowledge on the different Collection
s in java. I usually just go with ArrayList
, like in this case in Customer
for (mutable) jobs. Is there a better collection for this?