Have something loaded only when JList item is visibile
Posted
by elvencode
on Stack Overflow
See other posts from Stack Overflow
or by elvencode
Published on 2009-11-02T17:38:24Z
Indexed on
2010/05/15
3:44 UTC
Read the original article
Hit count: 226
Hello, i'm implementing a Jlist populated with a lot of elements. Each element corresponds to a image so i'd like to show a resized preview of them inside each row of the list. I've implemented a custom ImageCellRenderer extending the Jlabel and on getListCellRendererComponent i create the thumbnail if there'snt any for that element. Each row corresponds to a Page class where i store the path of the image and the icon applied to the JLabel. Each Page object is put inside a DefaultListModel to populate the JList. The render code is something like this:
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
Page page = (Page) value;
if (page.getImgIcon() == null)
{
System.out.println(String.format("Creating thumbnail of %s", page.getImgFilename()));
ImageIcon icon = new ImageIcon(page.getImgFilename());
int thumb_width = icon.getIconWidth() > icon.getIconHeight() ? 128 : ((icon.getIconWidth() * 128) / icon.getIconHeight());
int thumb_height = icon.getIconHeight() > icon.getIconWidth() ? 128 : ((icon.getIconHeight() * 128) / icon.getIconWidth());
icon.setImage(getScaledImage(icon.getImage(), thumb_width, thumb_height));
page.setImgIcon(icon);
}
setIcon(page.getImgIcon());
}
I was thinking that only a certain item is visibile in the List the cell renderer is called but i'm seeing that all the thumnails are created when i add the Page object to the list model. I've tried to load the items and after set the model in the JList or set the model first and after starting appending the items but the results are the same. Is there any way to load the data only when necessary or do i need to create a custom control like a JScrollPanel with stacked items inside where i check myself the visibility of each elements?
Thanks
© Stack Overflow or respective owner