public final class Book {
private final String title;
private final List<Author> listofAuthors;
public Book(String title, List<Author> listofAuthors)
{
this.title = title;
this. listofAuthors = listofAuthors;
}
public JComponent display() {
JPanel bookPanel = new JPanel();
bookPanel.add(new JLabel(title));
JList authorsList = new JList(); // Or similar
for (Author author: authors) {
authorsList.add(author.display());
}
bookPanel.add(authorsList);
return bookPanel;
}
}
Author class:
public final class Author
{
private final String firstname;
private final String lastname;
public Author(String firstname, String lastname)
{
this.firstname = firstname;
this.lastname = lastname;
}
//other methods
}
Question:
I'm following up on this post where I asked how to display data in a GUI without getters. My problem is, if I wanted to change an attribute, for example, remove an author from the list and display the change, how would I do it?
To know which author was selected from the JList I need to know what index the user selected. How would I get that if the JList is buried in the display method?
If I had a button, and used it to trigger an event after making a selection in the JList, where would that go?