Hello
I have a small problem with Drag and Drop images.
Class GridImage creates small thumbnails of images to 100 x 100px

Image drag and drop is defined by mouseListener and mousePressed event.

I know that actually I am copying component JLabel to class DrawImage with these settings.

What should I do in transferHandler to copy image without this resize ?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

class DragMouseAdapter extends MouseAdapter {
	public void mousePressed(MouseEvent e) {
		JComponent c = (JComponent) e.getSource();
		
		TransferHandler handler = c.getTransferHandler();
		handler.exportAsDrag(c, e, TransferHandler.COPY);
	}
}

public class GridImages extends JPanel {

	
	public JLabel[] images = new JLabel[9];	
	private JButton button1 = new JButton("Backgrounds");
	private JButton button2 = new JButton("More");
	private JButton button3 = new JButton("Titles");
	private JButton button4 = new JButton ("Next");
	private	JButton button5 = new JButton("Previous");
	private JLabel test = new JLabel();
	private File imageDir;
	private String setDir;
	private int n=0;
	private int p=0;
	private String[] filenames;
	private MouseListener listener = new DragMouseAdapter();
	
	private Toolkit toolkit = Toolkit.getDefaultToolkit();
	
	public GridImages(){
		super();
		setLayout(new GridLayout(5,3,10,10));
		
		
			
		button1.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				imageDir = new File("backgrounds");
				setDir = new String("backgrounds/");
				filenames = imageDir.list();
				if(n<filenames.length){
				LoadImages(setDir,filenames.length, filenames,n);
				}
				
			}

		});
		
		button2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				n=0;
				imageDir = new File("more");
				filenames = imageDir.list();
				LoadImages("more/", filenames.length, filenames,n);
				
			}

		});
		button3.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				
			}

		});
		
		button4.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				if(n<filenames.length){
				n+=3;
				
					LoadImages(setDir,filenames.length, filenames,n);
				}				
				
			}
		});
		
		
		button5.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				n-=3;
				LoadImages(setDir, filenames.length,filenames,n);
				
			}
		});
				
		for(int i=0; i<9; i++){
			images[i] = new JLabel();
			
		}
				
		add(button1);
		add(button2);
		add(button3);
		for(int i=0; i<9; i++){
			add(images[i]);
		}
		add(button5);
		add(test);
		add(button4);
		
	}
	public void LoadImages(String dir, int length, String[] filenames, int n){
		Image[] image = new Image[length];
		Image[] scaledImages = new Image[length];	
		ImageIcon[] icons = new ImageIcon[length];
		
		for(int i=0; i<9; i++){
			icons[i] = new ImageIcon();
		}
		
			for(int i=n; i<length; i++){
				image[i-n] = toolkit.getImage(dir+filenames[i]);
				scaledImages[i-n] = image[i-n].getScaledInstance(100,100 , Image.SCALE_FAST);
				icons[i-n].setImage(scaledImages[i-n]);
				
				images[i-n].setIcon(icons[i-n]);
				images[i-n].addMouseListener(listener);
				images[i-n].setTransferHandler(new TransferHandler("icon"));

			}		
		
	}
}

I can attach entire project.
http://www.megaupload.com/?d=S2KQRWSG

My problem:
When I am dragging small thumbnail from the right list to the left panel, image has the same size as thumbnail.
I want to drag it from thumbnail but display on layeredpane in full size.

Edited 5 Years Ago by Dersev: n/a

If it is too big to post on the forum, its too big.
Can you make a SSCCE that demonstrates the problem?

In my application
I have layeredpane, gridlayered 9 jlabels and 5 buttons.
When I am clicking on button images from local directory loads to JLabels scaled by

scaledImages[i-n] = image[i-n].getScaledInstance(100,100 , Image.SCALE_FAST);

I would like to drag scaled image to layeredpane with JLabel on it but without this scaling.

My drag and drop is based on MouseListener

images[i-n].addMouseListener(listener);				images[i-n].setTransferHandler(new TransferHandler("icon"));
scaledImages[i-n] = image[i-n].getScaledInstance(100,100 , Image.SCALE_FAST);

Are you asking to change what that statement does?

public void LoadImages(String dir, int length, String[] filenames, int n){
		Image[] image = new Image[length];
		Image[] scaledImages = new Image[length];	
		ImageIcon[] icons = new ImageIcon[length];
 
		for(int i=0; i<9; i++){
			icons[i] = new ImageIcon();
		}
 
			for(int i=n; i<length; i++){
				image[i-n] = toolkit.getImage(dir+filenames[i]);
				scaledImages[i-n] = image[i-n].getScaledInstance(100,100 , Image.SCALE_FAST);
				icons[i-n].setImage(scaledImages[i-n]);
 
				images[i-n].setIcon(icons[i-n]);
				images[i-n].addMouseListener(listener);
				images[i-n].setTransferHandler(new TransferHandler("icon"));
 
			}

This function loads images to thumbnails.

Here is declarations of this scaledImages

Image[] image = new Image[length];
		Image[] scaledImages = new Image[length];	
		ImageIcon[] icons = new ImageIcon[length];

And here is for loop to load and scale all images from array

for(int i=n; i<length; i++){
				image[i-n] = toolkit.getImage(dir+filenames[i]);
				scaledImages[i-n] = image[i-n].getScaledInstance(100,100 , Image.SCALE_FAST);
				icons[i-n].setImage(scaledImages[i-n]);
 
				images[i-n].setIcon(icons[i-n]);
				images[i-n].addMouseListener(listener);
				images[i-n].setTransferHandler(new TransferHandler("icon"));

If you don't want the images scaled, can you replace the statement at line 3?
Or why not take your images from the image array?

Yes but I would like to load small thumbnails and after dragging to layeredpane show full sized image.

Sorry, I don't understand your problem. You have both the scaled and full size images.
You want to use one here and the other there.
Why can't you change the code so it does what you want?

I have only full size images which I am scaling to thumbnails in my program.
When user will select interesting thumbnail, he or she is dragging this thumbnail to laeyredpane and show full size image. But my problem is when I am dragging my image from thumbnail it shows with the same size as thumbnail.

Is the problem connecting from the "dragged" image back to where it came from so you can get access to the full sized image?

Here is screenshot of my program
http://imageshack.us/photo/my-images/810/screenqc.jpg/

on the right side you have thumbnails which are full sized images but scaled.

And on the left side you have dragged first image from thumbnail.
But I don't know how to change transferhandler to transfer full sized image not scaled.

Edited 5 Years Ago by Dersev: n/a

Have you read the section of the java tutorial about Drag and Drop?
It mentions extending the TransferHandler class for custom transfers.
Is that what you want?
The user drags a thumbnail and a full image is copied to the target.

This article has been dead for over six months. Start a new discussion instead.