GWT image upload preview image before uploading to server - java

I am using GWT FileUpload() and a form to let the user select an image and upload it to the server. What I want to do is preview the image before it gets to the server. I am only able to get the file name from the FileUpload()
HorizontalPanel row = new HorizontalPanel();
row.add(accounts = new ListBox());
accounts.setWidth("200px");
row.setStyleName("accountPositioning");
accounts.setName("accounts");
final Image image = new Image();
image.setStyleName("previewImage");
setCellSpacing(10);
panel = new VerticalPanel();
panel.add(row);
panel.add(image);
final FormPanel form = new FormPanel();
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
downloadPanel = new FormPanel();
downloadPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
downloadPanel.setMethod(FormPanel.METHOD_GET);
deletePanel = new FormPanel();
deletePanel.setEncoding(FormPanel.ENCODING_MULTIPART);
deletePanel.setMethod(FormPanel.METHOD_POST);
upload = new FileUpload();
upload.setName("upload");
upload.setStyleName("chooseImageButton");
upload.setEnabled(false);
upload.setVisible(false);
VerticalPanel holder = new VerticalPanel();
uploadButton = new Button("Import");
uploadButton.setEnabled(false);
uploadButton.setStyleName("importImageButton");
uploadButton.setVisible(false);
uploadButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
String filename = upload.getFilename();
if (filename.length() == 0) {
Window.alert("No File Specified!");
} else {
int selectedIndex = accounts.getSelectedIndex();
accountIdStr = accounts.getValue(selectedIndex);
form.setAction(GWT.getModuleBaseURL()+"uploadfile" + "?entityId="+ accountIdStr);
form.submit();
}
}
});
How can i get the file path of the image file upload using GWT FileUpload() so i can preview the image before submitting it to the server?
I am using GWT 2.7.0 version so i cant use File, or Path library

You're going to need to get the File object out of the element...
And you can do it like this:
#Override
public elemental.html.FileList fileSelected(FileUpload fileUpload) {
final elemental.html.InputElement element = (elemental.html.InputElement) fileUpload.getElement();
return element.getFiles();
}
As you can see it uses gwt elemental.
Once you've extracted the File object you can do this:
import com.google.gwt.user.client.Element;
import elemental.html.File;
import elemental.html.ImageElement;
import elemental2.dom.FileReader;
public void loadPreview(File file) {
FileReader reader = new FileReader();
reader.onloadend = pe -> {
Element element0 = this.image.getElement();
com.google.gwt.dom.client.Element element = element0;
ImageElement image = (ImageElement) element;
image.setSrc(reader.result.asString());
image.addEventListener("load", evt -> {
LOG.debug("image loaded event {}", evt);
int owidth = image.getWidth();
int oheight = image.getHeight();
LOG.debug("widht {}', height {}", owidth, oheight);
//IMAGE_VIEW_PORT_WIDTH
//IMAGE_VIEW_PORT_HEIGHT
int height;
int width;
if (owidth >= (destAspectRatio) * oheight) {
height = (int) Math.round(IMAGE_VIEW_PORT_WIDTH * (oheight / (double) owidth));
width = IMAGE_VIEW_PORT_WIDTH;
} else {
width = (int) Math.round(IMAGE_VIEW_PORT_HEIGHT * (owidth / (double) oheight));
height = IMAGE_VIEW_PORT_HEIGHT;
}
image.setWidth(width);
image.setHeight(height);
this.image.setVisible(true);
LOG.debug("new width {}, new height {}", width, height);
});
return null;
};
reader.readAsDataURL((elemental2.dom.File) file);
}
I'm sorry it's so complex - it the code that I have and I think it figures out the correct view port size so that image is set to the correct size (though I'm not 100% sure).
You might get way without the image.addEventListener("load"...) handler as I believe it's the image.setSrc(reader.result.asString()) that is the money bit.
It's using a lot of elemental and elemental2 as stock gwt just doesn't give you enough exposure to the actual API you're dealing with.
Notice also that this uses the File object from elemental.

Related

Barcode Google Vision API check if detected barcode is inside area

I have problem with detecting if barcode is inside specified area. For testing purposes camera source preview and surface view has same size 1440x1080 to prevent scaling between camera and view. I get positive checks even if I see QR Code isn't in box what represents image. Whats wrong?
False positive check
ScannerActivity
public class ScannerActivity extends AppCompatActivity {
private static final String TAG = "ScannerActivity";
private SurfaceView mSurfaceView; // Its size is forced to 1440x1080 in XML
private CameraSource mCameraSource;
private ScannerOverlay mScannerOverlay; // Its size is forced to 1440x1080 in XML
#Override
protected void onCreate(Bundle savedInstanceState) {
// .. create and init views
// ...
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.ALL_FORMATS)
.build();
mCameraSource = new CameraSource.Builder(this, barcodeDetector)
.setRequestedPreviewSize(1440, 1080)
.setRequestedFps(20.0f)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setAutoFocusEnabled(true)
.build();
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
#Override
public void release() {
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
parseDetections(detections.getDetectedItems());
}
});
}
private void parseDetections(SparseArray<Barcode> barcodes) {
for (int i = 0; i < barcodes.size(); i++) {
Barcode barcode = barcodes.valueAt(i);
if (isInsideBox(barcode)) {
runOnUiThread(() -> {
Toast.makeText(this, "GOT DETECTION: " + barcode.displayValue, Toast.LENGTH_SHORT).show();
});
}
}
}
private boolean isInsideBox(Barcode barcode) {
Rect barcodeBoundingBox = barcode.getBoundingBox();
Rect scanBoundingBox = mScannerOverlay.getBox();
boolean checkResult = barcodeBoundingBox.left >= scanBoundingBox.left &&
barcodeBoundingBox.right <= scanBoundingBox.right &&
barcodeBoundingBox.top >= scanBoundingBox.top &&
barcodeBoundingBox.bottom <= scanBoundingBox.bottom;
Log.d(TAG, "isInsideBox: "+(checkResult ? "YES" : "NO"));
return checkResult;
}
}
Explanation to your issue is simple, but the solution is not trivial to explain.
The coordinates of the box from your UI will mostly not the be the same like the imaginary box on each preview frame. You must transform the coordinates from the UI box to scanBoundingBox.
I open sourced an example which implement the same usecase you are trying to accomplish. In this example I took another approach, I cut the box out of each frame first before feeding it to Google Vision, which is also more efficient, since Google Vision don't have to analyse the whole picture and waste tons of CPU...
I decied to cropp frame by wrapping barcode detector however I don't know why but cropped frame is rotated by 90 degress even smarphone is upright orientation.
Box Detector class
public class BoxDetector extends Detector<Barcode> {
private Detector<Barcode> mDelegate;
private int mBoxWidth;
private int mBoxHeight;
// Debugging
private CroppedFrameListener mCroppedFrameListener;
public BoxDetector(Detector<Barcode> delegate, int boxWidth, int boxHeight) {
mDelegate = delegate;
mBoxWidth = boxWidth;
mBoxHeight = boxHeight;
}
public void setCroppedFrameListener(CroppedFrameListener croppedFrameListener) {
mCroppedFrameListener = croppedFrameListener;
}
#Override
public SparseArray<Barcode> detect(Frame frame) {
int frameWidth = frame.getMetadata().getWidth();
int frameHeight = frame.getMetadata().getHeight();
// I assume that box is centered.
int left = (frameWidth / 2) - (mBoxWidth / 2);
int top = (frameHeight / 2) - (mBoxHeight / 2);
int right = (frameWidth / 2) + (mBoxWidth / 2);
int bottom = (frameHeight / 2) + (mBoxHeight / 2);
YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21, frameWidth, frameHeight, null);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(left, top, right, bottom), 100, outputStream);
byte[] jpegArray = outputStream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);
Frame croppedFrame = new Frame.Builder()
.setBitmap(bitmap)
.setRotation(frame.getMetadata().getRotation())
.build();
if(mCroppedFrameListener != null) {
mCroppedFrameListener.onNewCroppedFrame(croppedFrame.getBitmap(), croppedFrame.getMetadata().getRotation());
}
return mDelegate.detect(croppedFrame);
}
public interface CroppedFrameListener {
void onNewCroppedFrame(Bitmap bitmap, int rotation);
}
}
Box Detector usuage
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.ALL_FORMATS)
.build();
BoxDetector boxDetector = new BoxDetector(
barcodeDetector,
mBoxSize.getWidth(),
mBoxSize.getHeight());
boxDetector.setCroppedFrameListener(new BoxDetector.CroppedFrameListener() {
#Override
public void onNewCroppedFrame(final Bitmap bitmap, int rotation) {
Log.d(TAG, "onNewCroppedFrame: new bitmap, rotation: "+rotation);
runOnUiThread(new Runnable() {
#Override
public void run() {
mPreview.setImageBitmap(bitmap);
}
});
}
});
Cropped frame is rotated

Image in my application?

I have the following problem, my application retrieves data from a database online as well as the texts there are also the images I am viewing via the following class. The images are displayed correctly but not to their size, I'd like that resize based on the size of the display, you know how can I do? The source is this:
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html.ImageGetter;
class HttpImageGetter implements ImageGetter {
#Override
public Drawable getDrawable(String source) {
try {
URL url = new URL(source);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
BitmapDrawable dr = new BitmapDrawable(BitmapFactory.decodeStream(is));
dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
return dr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
I also have a second problem. The opening of the text is slower as there are also images that weigh more though if I click again on the name of the text shows a screen all black and sometimes it says that the application is not responding. All this happens especially when I'm with Edge connection. Do you have ideas on how I can fix and speed up the opening of the text?
PS: The images are contained in a TextView
private TextView htmlTextView;
private SpannableStringBuilder htmlSpannable;
#Override
public void onCreate(Bundle savedInstanceState) {
// ...
// first parse the html
// replace getHtmlCode() with whatever generates/fetches your html
Spanned spanned = Html.fromHtml(getHtmlCode());
// we need a SpannableStringBuilder for later use
if (spanned instanceof SpannableStringBuilder) {
// for now Html.fromHtml() returns a SpannableStringBuiler
// so we can just cast it
htmlSpannable = (SpannableStringBuilder) spanned;
} else {
// but we have a fallback just in case this will change later
// or a custom subclass of Html is used
new SpannableStringBuilder(spanned);
}
// now we can call setText() on the next view.
// this won't show any images yet
htmlTextView.setText(htmlSpannable);
// next we start a AsyncTask that loads the images
new ImageLoadTask().execute();
// ...
}
private class ImageLoadTask extends AsyncTask {
DisplayMetrics metrics = new DisplayMetrics();
#Override
protected void onPreExecute() {
// we need this to properly scale the images later
getWindowManager().getDefaultDisplay().getMetrics(metrics);
}
#Override
protected Void doInBackground(Void... params) {
// iterate over all images found in the html
for (ImageSpan img : htmlSpannable.getSpans(0,
htmlSpannable.length(), ImageSpan.class)) {
if (!getImageFile(img).isFile()) {
// here you have to download the file
}
// we use publishProgress to run some code on the
// UI thread to actually show the image
// -> onProgressUpdate()
publishProgress(img);
}
return null;
}
#Override
protected void onProgressUpdate(ImageSpan... values) {
// save ImageSpan to a local variable just for convenience
ImageSpan img = values[0];
// now we get the File object again. so remeber to always return
// the same file for the same ImageSpan object
File cache = getImageFile(img);
// if the file exists, show it
if (cache.isFile()) {
// first we need to get a Drawable object
Drawable d = new BitmapDrawable(getResources(),
cache.getAbsolutePath());
// next we do some scaling
int width, height;
int originalWidthScaled = (int) (d.getIntrinsicWidth() * metrics.density);
int originalHeightScaled = (int) (d.getIntrinsicHeight() * metrics.density);
if (originalWidthScaled > metrics.widthPixels) {
height = d.getIntrinsicHeight() * metrics.widthPixels
/ d.getIntrinsicWidth();
width = metrics.widthPixels;
} else {
height = originalHeightScaled;
width = originalWidthScaled;
}
// it's important to call setBounds otherwise the image will
// have a size of 0px * 0px and won't show at all
d.setBounds(0, 0, width, height);
// now we create a new ImageSpan
ImageSpan newImg = new ImageSpan(d, img.getSource());
// find the position of the old ImageSpan
int start = htmlSpannable.getSpanStart(img);
int end = htmlSpannable.getSpanEnd(img);
// remove the old ImageSpan
htmlSpannable.removeSpan(img);
// add the new ImageSpan
htmlSpannable.setSpan(newImg, start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// finally we have to update the TextView with our
// updates Spannable to display the image
htmlTextView.setText(htmlSpannable);
}
}
private File getImageFile(ImageSpan img) {
// you need to implement this method yourself.
// it must return a unique File object (or something
// different if you also change the rest of the code)
// for every image tag. use img.getSource() to get
// the src="" attribute. you might want to use some
// hash of the url as file name
}
}
to resize image the codes included are you can change the logic as you want 20 a scaling % or whatever you want as you have the display matrics (hight and width of screen) enjoy
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width, height;
int originalWidthScaled = (int) (d.getIntrinsicWidth() * metrics.density);
int originalHeightScaled = (int) (d.getIntrinsicHeight() * metrics.density);
if (originalWidthScaled > metrics.widthPixels) {
height = d.getIntrinsicHeight() * metrics.widthPixels
/ d.getIntrinsicWidth();
width = metrics.widthPixels;
} else {
height = originalHeightScaled;
width = originalWidthScaled;
}
// it's important to call setBounds otherwise the image will
// have a size of 0px * 0px and won't show at all
d.setBounds(0, 0, width, height);
and loading the image in seprate thread will avoid anr

GWT how to make a clickhandler inside my arrayList

I have this simple ArrayList
Image img = new Image("/Mobile 005.JPG");
Image img1 = new Image("/Mobile 006.JPG");
Image img2 = new Image("/Mobile 007.JPG");
Image img3 = new Image("/Mobile 008.JPG");
imgList.add(img);
imgList.add(img1);
imgList.add(img2);
imgList.add(img3);
HorizontPanel hpnl = new HorizontalPanel();
hpnl.add(imgList);
This list of images will be coming from db and can be of any number.
At this time this HorizontalPanel contains 4 images (it may contain 400 images in future), Now if a user comes and click on the second image lets say , how will we know which image has been clicked by the user?
where and how will I put my clickHandler?
You will get to know which image is clicked by using the getSource() API as -
Image img = new Image("/Mobile 005.JPG");
img.addClickHandler( getClickHandler() );
Image img1 = new Image("/Mobile 006.JPG");
img1.addClickHandler( getClickHandler() );
Image img2 = new Image("/Mobile 007.JPG");
img2.addClickHandler( getClickHandler() );
Image img3 = new Image("/Mobile 008.JPG");
img3.addClickHandler( getClickHandler() );
imgList.add(img);
imgList.add(img1);
imgList.add(img2);
imgList.add(img3);
HorizontPanel hpnl = new HorizontalPanel();
hpnl.add(imgList);
ClickHandler imageClickHandler;
private ClickHandler getClickHandler()
{
if( imageClickHandler != null)
{
return imageClickHandler;
}
imageClickHandler = new ClickHandler()
{
public void onClick( ClickEvent event )
{
Image source = (Image)event.getSource();
// This is the source that has caused the event.
}
};
return imageClickHandler;
}
you can also try with following.
HorizontalPanel hp = new HorizontalPanel();
hp.add(getImage("/Mobile 005.JPG"));
hp.add(getImage("/Mobile 006.JPG"));
hp.add(getImage("/Mobile 007.JPG"));
hp.add(getImage("/Mobile 008.JPG"));
private Image getImage(String imagePath){
Image image = new Image(imagePath);
image.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// write the on click code.
}
});
}
From the imagepath variable, you can get to know which onclick is called.
Are looking for something like this
List<Image> images = new ArrayList<Image>();
images.add(new Image());
images.add(new Image());//Into this list add your DB images list
images.add(new Image());
for (Image image : images) {
image.addClickHandler(
//singleton instance of clickhandler
});
}

stitch images together in java

I'm trying to stitch some images together using java. I have a bunch of images I'd like to stitch together and they are all the same dimensions so it's really just a question of lining them up next to each other I suppose. I have it working but it's very slow and probably very memory intensive. I'm wondering if there's an easier way:
public static void main(String[] args) throws IOException
{
int dim = 256;
BufferedImage merged = null;
for(int y = 0; y<10;y++)
{
for(int x = 0; x<10;x++)
{
URL url = new URL(someURL);
BufferedImage nextImage = ImageIO.read(url);
if(merged==null)
merged=nextImage;
else
{
BufferedImage tempMerged;
tempMerged = new BufferedImage(10*dim,10*dim,merged.getType());
//Write first image
for(int xx=0;xx<merged.getWidth();xx++)
for(int yy=0;yy<merged.getHeight();yy++)
tempMerged.setRGB(xx,yy,merged.getRGB(xx,yy));
//Write img2
for(int xx=0;xx<dim;xx++)
{
for(int yy=0;yy<dim;yy++)
{
int destX = (x*dim)+xx;
int destY = (y*dim)+yy;
tempMerged.setRGB(destX,destY,nextImage.getRGB(xx,yy));
}
}
merged=tempMerged;
}
System.out.println("Stitched image at "+x+","+y);
}
}
ImageIO.write(merged, "png", new File("merged.png"));
}
#Thomas: You'd have to create a new image of twice the size of the source images (e.g. for 2x 512x512 the new image should be 512x1024 or 1024x512). Then you'd render the source images to the respective area/rectangle of the target image
E.G. TiledImageWrite.java
import java.awt.image.BufferedImage;
import java.awt.*;
import javax.swing.*;
import java.net.URL;
import java.io.File;
import javax.imageio.ImageIO;
class TiledImageWrite {
public static void main(String[] args) throws Exception {
URL dayStromloUrl = new URL("https://i.stack.imgur.com/OVOg3.jpg");
URL nightStromloUrl = new URL("https://i.stack.imgur.com/lxthA.jpg");
final BufferedImage dayStromloImage = ImageIO.read(dayStromloUrl);
final BufferedImage nightStromloImage = ImageIO.read(nightStromloUrl);
final int width = dayStromloImage.getWidth();
final int height = dayStromloImage.getHeight();;
final BufferedImage columnImage =
new BufferedImage(width,2*height,BufferedImage.TYPE_INT_RGB);
final BufferedImage rowImage =
new BufferedImage(2*width,height,BufferedImage.TYPE_INT_RGB);
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JPanel gui = new JPanel(new BorderLayout(3,3));
Graphics2D g2dColumn = columnImage.createGraphics();
g2dColumn.drawImage(dayStromloImage,0,0, null);
// start this one at 'height' down the final image
g2dColumn.drawImage(nightStromloImage,0,height, null);
Graphics2D g2dRow = rowImage.createGraphics();
g2dRow.drawImage(dayStromloImage,0,0, null);
// start this one at 'width' across the final image
g2dRow.drawImage(nightStromloImage,width,0, null);
gui.add(new JLabel(new ImageIcon(columnImage)),BorderLayout.CENTER);
gui.add(new JLabel(new ImageIcon(rowImage)),BorderLayout.SOUTH);
JOptionPane.showMessageDialog(null, gui);
}
} );
ImageIO.write(columnImage, "png", new File("column.png"));
ImageIO.write(rowImage, "png", new File("row.png"));
}
}
column.png
AFAIK what you're doing here is to write layers to a image. However, the png format doesn't support this.
You'd have to create a new image of twice the size of the source images (e.g. for 2x 512x512 the new image should be 512x1024 or 1024x512). Then you'd render the source images to the respective area/rectangle of the target image.
I figured out why it was going slow. In reality, I didn't want to merge images together, but rather stitch together a bunch of images. What I was doing was rewriting the original image everything when all I really want to do is add to it. Much faster now!

Drop File on SWT Label with Image (DND)

Problem description:
The user should be able to drag an Image-File from his computer to a RCP Application. The drop-target is a SWT-Label which is generated through the Eclipse FormToolkit. (Eclipse Forms)
With the following code, the user is able to drag Image-Files as well as Images from a Browser and drop them on the label (works well).
The problem occurs, when the label shows a image:
lblImage.setImage()
In my example, I set the image of the label, after the user dropped a file. As a consequence, subsequent drags are no longer registered.
(dragEnter method is no longer invoked)
/** create label **/
Label lblImage = fFormToolkit.createLabel(fForm.getBody(), "");
GridData gd = new GridData();
gd.widthHint = 200;
gd.heightHint = 200;
lblImage.setLayoutData(gd);
/** drag drop support **/
int ops = DND.DROP_COPY | DND.DROP_LINK | DND.DROP_DEFAULT;
final FileTransfer fTransfer = FileTransfer.getInstance();
final ImageTransfer iTransfer = ImageTransfer.getInstance();
Transfer[] transfers = new Transfer[] { fTransfer, iTransfer };
DropTarget target = new DropTarget(fLblArtWork, ops);
target.setTransfer(transfers);
target.addDropListener(new DropTargetAdapter() {
#Override
public void drop(DropTargetEvent event) {
if (event.data instanceof String[]) {
String[] filenames = (String[]) event.data;
if (filenames.length > 0){
Image i = new Image(Display.getCurrent(), filepath);
lblImage.setImage(i);
}
} else if (event.data instanceof ImageData) {
Image i = new Image(Display.getCurrent(), data);
lblImage.setImage(i);
}
}
public void dragEnter(DropTargetEvent event) {
System.out.println("drag enter");
event.detail = DND.DROP_COPY;
}
});
Question: How do I register dragEnter Events on a SWT Label that shows an Image?
Thanks
In your example there were some problems that caused this not to compile for me. After I fixed the issues I was able to drag png files onto the component and each successive drop changed the image correctly.
Here are the changes:
Original
DropTarget target = new DropTarget(fLblArtWork, ops);
became:
DropTarget target = new DropTarget(lblImage, ops);
Original
Image i = new Image(Display.getCurrent(), filepath);
became:
Image i = new Image(Display.getCurrent(), filenames[0]);
Original
Image i = new Image(Display.getCurrent(), data);
became
Image i = new Image(Display.getCurrent(), (ImageData) event.data);
I also create my label the following way:
final Label lblImage = new Label(shell, SWT.NONE);
but that shouldn't make a difference.
I used SashForm here to set an background image from the local system. AS per your requirement I have done the text and label also but I didn't set. You can set it by the labelobject.setImage(image);
final SashForm sashForm = new SashForm(composite, SWT.BORDER);
sashForm.setBounds(136, 10, 413, 237);
final Label lblHello = new Label(composite, SWT.NONE);
DragSource dragSource = new DragSource(lblHello, DND.DROP_NONE);
ImageTransfer imgTrans=ImageTransfer.getInstance();
FileTransfer fileTrans=FileTransfer.getInstance();
Transfer[] transfer=new Transfer[] { fileTrans,imgTrans,TextTransfer.getInstance() };
DropTarget dropTarget = new DropTarget(sashForm, DND.DROP_NONE);
dropTarget.setTransfer(transfer);
dragSource.setTransfer(transfer);
lblHello.setBounds(27, 219, 55, 15);
lblHello.setText("Hello");
dragSource.addDragListener(new DragSourceAdapter() {
#Override
public void dragStart(DragSourceEvent event) {
event.doit=true;
}
});
//Drop Event
dropTarget.addDropListener(new DropTargetAdapter() {
#Override
public void drop(DropTargetEvent event) {
System.out.println(event.detail);
//String path = System.getProperty("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");
Image image=new Image(display, "C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");
sashForm.setBackgroundImage(image);
}
});
Easy Way : Drop File on SWT Label with Image (DND)
The drop event occurs when the user releases the mouse over the Drop target.
final CLabel lblNewLabel = new CLabel(parent, SWT.BORDER);
lblNewLabel.setBounds(10, 43, 326, 241);
lblNewLabel.setText("Drop Target");
// Allow data to be copied or moved to the drop target
DropTarget dropTarget = new DropTarget(lblNewLabel, DND.DROP_MOVE| DND.DROP_COPY | DND.DROP_DEFAULT);
// Receive data in Text or File format
final TextTransfer textTransfer = TextTransfer.getInstance();
final FileTransfer fileTransfer = FileTransfer.getInstance();
Transfer[] types = new Transfer[] {fileTransfer, textTransfer};
dropTarget.setTransfer(types);
// DropTargetEvent
dropTarget.addDropListener(new DropTargetAdapter() {
#Override
public void drop(DropTargetEvent event) {
if (textTransfer.isSupportedType(event.currentDataType)) {
String text = (String)event.data;
lblNewLabel.setText(text);
}
if (fileTransfer.isSupportedType(event.currentDataType)){
//clear Label Text
lblNewLabel.setText("");
//list out selected file
String[] files = (String[])event.data;
for (int i = 0; i < files.length; i++) {
String[] split = files[i].split("\\.");
String ext = split[split.length - 1];
// Set Images format "jpg" and "png"
if(ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("png"))
{
lblNewLabel.setImage(SWTResourceManager.getImage(files[i]));
}
else
{
lblNewLabel.setText(files[i]);
}
}//end for loop
}
}//End drop()
});//End addDropListener

Categories

Resources