I have a Book object with several different String fields (title, author, subject...), and I am deigning a GUI to display each of these fields for a collection of books. I have a panel on which I want to have, for each field, a label (naming the field), a TextField (which displays the value stored in the field), and an edit button, which makes the TextField editable and displays a second button to save any edits made.
Rather than coding each of these into the panel individually, I would like to create a "field panel" with a single label, TextField and button. Then I want to use a foreach loop to create one of these panels for each field (stored in a list) and load these panels into the main panel.
I know that the following is not possible:
Book b = new Book();
String[] fieldTitles = {title, author, subject, publisher};
for (String str : fieldTitles) {
FieldPanel fp = new FieldPanel();
fp.namelabel.settext(str);
fp.textField.settext(b.str);
}
But is there another way to achieve what I'm trying to do here on the last line, which is to use a named String variable to refer to a field of an object? In other words, I want to do: objectInstance.stringVariable (where stringVariable matches the name of a field of the objectInstance.
Field.