modmanager.swing.ImageListRenderer.java Source code

Java tutorial

Introduction

Here is the source code for modmanager.swing.ImageListRenderer.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modmanager.swing;

import java.awt.Component;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import net.coobird.thumbnailator.Thumbnailator;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author ruman
 */
public class ImageListRenderer extends javax.swing.JPanel implements ListCellRenderer<String> {
    private HashMap<String, BufferedImage> images = new HashMap<>();

    /**
     * Creates new form ImageListRenderer
     */
    public ImageListRenderer() {
        initComponents();
    }

    public void setImages(HashMap<String, BufferedImage> img) {
        images = img;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileNameLabel = new javax.swing.JLabel();
        imagePanel = new modmanager.swing.ImagePanel();

        fileNameLabel.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(imagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(fileNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(fileNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                .addComponent(imagePanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel fileNameLabel;
    private modmanager.swing.ImagePanel imagePanel;
    // End of variables declaration//GEN-END:variables

    @Override
    public Component getListCellRendererComponent(JList<? extends String> jlist, String e, int i,
            boolean isSelected, boolean isFocused) {
        if (isSelected) {
            this.setBackground(UIManager.getColor("List.selectionBackground"));
        } else {
            this.setBackground(UIManager.getColor("List.background"));
        }

        imagePanel.setImage(images.get(e));
        fileNameLabel.setText(FilenameUtils.getBaseName(e));

        return this;
    }
}