Portapapeles.java

De ChuWiki
Saltar a: navegación, buscar

Clase de ejemplo del tutorial Drag and Drog (Arrastrar y Soltar)

/*==============================================================================
*
* $RCSfile: cvsmkhd,v $
*
* $Revision: 1.2 $ $Date: 2003/09/22 07:05:39 $
*
* $State: Exp $
*
* AUTOR: paquito
*
==============================================================================*/

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Portapapeles {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		  JFrame frame = new JFrame(  );
	      final JLabel jLabelText = new JLabel();
	      final JLabel jLabelImage = new JLabel(  );
	      

	      JButton jButtonAbrirFichero = new JButton( "Cargar Imagen" );
	      jButtonAbrirFichero.addActionListener( 
	         new ActionListener(  )
	         {
	            public void actionPerformed( ActionEvent arg0 )
	            {
	          	
					try {
						JFileChooser chooser = new JFileChooser();
						FileNameExtensionFilter filter = new FileNameExtensionFilter(
								"JPG & GIF Images", "jpg", "gif");
						chooser.setFileFilter(filter);
						int returnVal = chooser.showOpenDialog((Component) arg0
								.getSource());
						if (returnVal == JFileChooser.APPROVE_OPTION) {
							Image image = Toolkit.getDefaultToolkit().getImage(
									chooser.getSelectedFile().getCanonicalPath());
							jLabelImage.setIcon(new ImageIcon(image));
							jLabelText.setText(chooser.getSelectedFile().getCanonicalPath() );
						}
					} catch (Exception e) 
					{
						// TODO: handle exception
					}            	  
	            }
	         } );

	      JButton jButtonPegar = new JButton( "Pegar desde el portapapeles" );
	      jButtonPegar.addActionListener( 
	         new ActionListener(  )
	         {
	            public void actionPerformed( ActionEvent arg0 )
	            {
	               Clipboard clipboard =
	                  Toolkit.getDefaultToolkit(  ).getSystemClipboard(  );
	               Transferable transferable = clipboard.getContents( null );

	               if( transferable.isDataFlavorSupported( DataFlavor.imageFlavor ) )
	               {
	                  try
	                  {
	                     Image image =
	                        (Image)transferable.getTransferData( 
	                           DataFlavor.imageFlavor );
	                     jLabelImage.setIcon( new ImageIcon( image ) );
	                  }
	                  catch( Exception exception )
	                  {
	                     System.out.println( exception );
	                  }
	               }

	               if( transferable.isDataFlavorSupported( DataFlavor.stringFlavor ) )
	               {
	                  try
	                  {
	                     String texto =
	                        (String)transferable.getTransferData( 
	                           DataFlavor.stringFlavor );
	                     jLabelText.setText( texto );
	                  }
	                  catch( Exception exception )
	                  {
	                     System.out.println( exception );
	                  }
	               }
	               else
	               {
	            	   jLabelText.setText(null);
	               }
	            }
	         } );

	      JButton jButtonCopiar = new JButton( "Copiar al portapapeles" );
	      jButtonCopiar.addActionListener( 
	         new ActionListener(  )
	         {
	            public void actionPerformed( ActionEvent arg0 )
	            {
	               Clipboard clipboard =
	                  Toolkit.getDefaultToolkit(  ).getSystemClipboard(  );
	               ImageSelection imageSelected= new ImageSelection(((ImageIcon)jLabelImage.getIcon()).getImage(), jLabelText.getText());
	               clipboard.setContents(imageSelected, null);
	            }
	         } );

	      frame.add( jLabelText, BorderLayout.NORTH);
	      frame.add( jLabelImage );
	      
	      
	      JPanel panel = new JPanel();
	      panel.add( jButtonPegar);
	      panel.add (jButtonCopiar);
	      panel.add( jButtonAbrirFichero);
	      
	      frame.add( 
	         panel,
	         BorderLayout.SOUTH );
	      
	      frame.setVisible( true );
	      frame.pack(  );
	}

}