Download something only when the JList element is visible

I am implementing a Jlist filled with many elements. Each element corresponds to an image, so I would like to show its dimensional view in each line of the list. I implemented a custom ImageCellRenderer extending Jlabel and getListCellRendererComponent . I create a thumbnail if this item does not exist. Each line corresponds to the Page class , where I store the image path and icon applied to JLabel. Each Page object is placed inside the DefaultListModel to populate the JList. The visualization code looks 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());

    }

, , , thumnails , "" . JList , , . , , , JScrollPanel , ?

+3
2

, JList , .

, , setPrototypeCellValue (...). , , .

+3

BasicListUI updateLayoutState(). dataModel, updateLayoutStateNeeded ( , dataModel). getListCellRendererComponent . , fixedCellHeight fixedCellWidth JList.

:

  • setFixedCellHeight(int)
  • setFixedCellWidth(int)

: , , , . , . , , . , " ", , Page , , , , , " ", Page Page . .

+1
source

Source: https://habr.com/ru/post/1721719/


All Articles