JListに登録するリスナーはListSelectionListenerで、選択が変更されたときに警告が表示されます。私はこれをやっていたならば、私は次のような何かをするだろう:
public class BookListModel {
public List<Book> getBooks() {
// Replace with however you get your books
return Arrays.asList(new Book("It", "Stephen King"),
new Book("The Lion, The Witch, and the Wardrobe", "C.S. Lewis"));
}
}
public class Book {
private String title;
private String author;
public String getTitle() { return title; }
public String getAuthor() { return author; }
public Book(String title, String author) {
this.title = title;
this.author = author;
}
}
public class BookListView extends JPanel {
private JList books;
private BookInfoView bookInfo;
private BookListModel model;
public BookListView(BookListModel model) {
books = new JList(model.toArray());
bookInfo = new BookInfoView();
books.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// get the book that was clicked
// call setBook on the BookInfoView
}
});
// Add the JList and the info view
}
}
public class BookInfoView extends JPanel {
private JLabel titleLabel;
private JLabel authorLabel;
private JTextField titleTextField;
private JTextField authorTextField;
public void setBook(Book b) {
// adjust the text fields appropriately
}
}
は、上記の本のリストが静的であることを前提としています。そうでない場合は、BookListModelをDefaultListModelまで拡張して、適切なメソッドを記入してください。