Reputation: 1133
I have a panel, let's call it detailsPanel
, which holds a Person
reference and displays its field values in the following manner:
Name: person.getName ();
Surname: person.getSurname ();
Emain: person.getEmail ();
.... .......
.... .......
And so on. I will use JLabels
(correctly aligned using a GridBagLayout
) to show each (fieldName
, fieldValue
). I have a lot of fields to display.
The problem is that the panel which shows the details must be always visible, i.e it will not be shown in a modal JDialog
, so that i could create the panel by simply reading my Person
object fields at the panel creation.
The panel must always be visible, and its Person
reference will change when the user selects a different row in a Person
list. This means i will call a method to update its state, something like:
detailsPanel.setPerson (aPerson);
Now, i'm wondering how i should update all the fields. Should i keep a reference to all the JLabels
which show the values, and use setText(value)
on each of them when i update the panel, or would it be better to override getText()
method for every label, returning the correct field value, so that in the update method i would only repaint the panel, and the text would automatically change when the getter method is used on a different Person
object?
Any suggestion is appreciated!
Upvotes: 1
Views: 159
Reputation: 5841
Since this is UI stuff which is usually called almost never (relative to how often things are called in other computation) you don't need to worry about efficiency at all. Just do what you think is the most elegant solution. There are three options That quickly come to my mind. They are ordered from quick and static to elegant and reusable:
Quick and dirty: create your constructor and make everything look nice. Then move everything from the constructor to a separate init()
method and every time the entities change, you just call removeAll();
and then init()
again.
As you suggested, keep a reference to all labels and use the setPerson()
method to update all panels. Then call this method in the constructor (this is arguably the most common solution).
As you suggested, build your own extension of JLabel
. This new class should either have an update()
method which is to be called when things change, or have it set its own listeners to ensure that it gets notified of any relevant change.
If you are planning to create a single panel which is supposed to display all kinds of objects, you could have those object implement an interface called Displayable
which gives you generic access to all its values and maybe even listeners to each value. An alternative to the Displayable
interface is to use reflection and use annotations to allow the panel to get its values for display.
Please note that the most elegant solution is - contrary to what some people may tell you - not always the best for any situation. How much maintenance do you expect there to be in the future? How big is the application? Will you ever hand off the code to someone else? All these and more need to be considered to decide how "nice" you want your solution to be.
Upvotes: 1