Java renderer with detail example

Java renderer

-In JList we have learned how to create a simple JList with some capture events. In this article we will continue to learn how to customize JList to create a list of elements of ours. For example, contacts (photo, contact name, phone number) or favorite book list (including book cover, book title, author's name).

-Let's first create a Book class to store the book information. In this book class, there are three attributes: book title, author, and booklet name with the initialization, getter, setter methods as follows:
java renderer
-Next we create a JFrame that displays our book list. In the JFrame contains a JPanel which is main panel, JPanel contains a JList which is listBook, listBook is placed in a JScrollPane to scroll up and down in case the long list that JList can not be displayed.

File name: JListCustomRenderer.java
java renderer
Continue JListCustomRenderer.java
java renderer
-Finished, so we have a list of books already, run the program to see how it? =)) very unexpected, not as expectation at all.
java renderer
-Why is that, we have added to the list of books including the title and author, So why does the program show like that? Actually, JList is handled through the ListModel and moreover the JList's objects is shown by an object called a Renderer and for every JList it has a default Renderer. How does this default Renderer work? The renderer is responsible for displaying the elements in JList. It does this by calling the toString () method of the object in JList and getting the string retrieved to show up. And because in the Book class we did not write the toString () method, then the Renderer took the string of the defaults toString () method in the Book class. And the strings we get in the program are also the strings that the system provides for each Book object in JList.

-Now let's try to override the toString () method in the Book class as follows:
java renderer
-We override the toString () method to return the name and author of the book. So when running the program we get a List as follows:
java renderer

Use of java renderer

-It seems we've all been okay? Not yet! We need to display the book cover as the opening image, how to do? As we all know the display of JList is due to the java Renderer then when we want to change it is quite easy by rewriting the JList Renderer according to our wishes.
Now let's define what each element in JList is. Here we want it to be a JPanel containing three JLabels, one JLabel is the cover of the book, one JLabel is the book's title and the last JLabel is the author's name in blue.
-To customize the java Renderer we will add another class named BookRenderer that inherits from a JPanel. Also, this class implements an Interface called ListCellRenderer. This interface has a getListCellRendererComponent () method that returns our JPanel and it is processed for display to JList.
java renderer
-Each element in JList is a JPanel. This JPanel has a layout that is BorderLayout, which contains an icon in the west and a panelText. panelText is a JPanel containing two JLabel titles and author names.
We put the text as well as the icon for the JLabel in the getListCellRendererComponent () method. This method has a Book object that is passed to the book. The main object of this book is the element in JList that is displayed. So we put the icons, text of the JLabel through the iconName, name, author of this book object.
-Now we go back to the JListCustomRenderer class and revise the createListBook method as follows:
java renderer
-Now run the program and we will have a JList as desired.
java renderer
-But not finished yet. You notice that when we click on an item, there is no phenomenon @@. This is because we have rewritten the java Renderer so the effects that java Renderer default creates are no longer available (background color when selecting an item). We will set the background color for JPanel, for JLabel when it is selected or not selected as follows (written in the getRistCellRendererComponent () method of the BookRenderer class).
java renderer
-Now, we can select items.
java renderer
-Read more how to create java swing flowlayout with detail examples:

Comments