I needed to implement a list of images, that the user can move wth drag and drop. That's the code I'm using :
...
model = new DefaultListModel();
imageList = new JList(model);
imageList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
imageList.setTransferHandler(new ListItemTransferHandler());
imageList.setDropMode(DropMode.INSERT);
imageList.setDragEnabled(true);
imageList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
imageList.setVisibleRowCount(1);
imageList.setFixedCellWidth(405);
imageList.setFixedCellHeight(height);
imageList.setCellRenderer(new IconCellRenderer());
then I load my files as BufferedImages and add them to the model
my IonCellRenderer class is the following :
class IconCellRenderer extends DefaultListCellRenderer {
private static final long serialVersionUID = 1L;
private int size1;
private int size2;
private int spostamento;
private BufferedImage icon;
IconCellRenderer() {
this(400,600);
}
IconCellRenderer(int size1,int size2) {
this.size1 = size1;
this.size2 = size2;
spostamento = 4;
icon = new BufferedImage(size1,size2,BufferedImage.TYPE_INT_ARGB);
}
@Override
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (c instanceof JLabel && value instanceof BufferedImage) {
JLabel l = (JLabel)c;
l.setText("");
BufferedImage i = (BufferedImage)value;
l.setIcon(new ImageIcon(icon));
Graphics2D g = icon.createGraphics();
g.setColor(new Color(0,0,0,0));
g.clearRect(spostamento, 0, size1, size2);
g.drawImage(i,spostamento,0,size1,size2,this);
g.dispose();
}
return c;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(size1, size2);
}
}
However when I run the code, it works but the displayed images have such a bad quality, and also they are not displaying some lines. I don't know if it could be because of the size of the images is kind of big (i.e. 1488x2105) and I resize them to 400x600.
How can I solve?
Thanks in advice, Stefano Franchini