Reputation: 1796
I am displaying an image in a button and when the button is clicked it opens a composite that allows me to select another picture to use for the button. Upon the save action the the code below resizes the picture and saves it to a directory. Then it loads the reloads the composite. When the composite loads the button pulls its image from a default location based on the id of the record the user is viewing. The problem I am having is that the image on the button remains the same unless I close and reload the application. An interesting thing to note is that when the default button image is loaded (i.e. there is no picture saved for its id) the button image changes as it should but only the first time. Hope I have described my problem clear enough if you need me to clarify please comment.
--- Code For Save Button --
Button btnSave = new Button(this, SWT.NONE);
btnSave.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String path = txtPhotoPath.getText();
if (CC_Files.fileExists(path)) {
ArrayList<String> picTypes = new ArrayList<String>();
picTypes.add(".jpg");
picTypes.add(".png");
picTypes.add(".gif");
int t = 0;
for(int i = 0; i < picTypes.size(); i++){
String s = picTypes.get(i);
if(path.contains(s.toUpperCase())){
t++;
}
if(path.contains(s.toLowerCase())){
t++;
}
}
if (t > 0) {
Image image = (Image) SWTResourceManager.getImage(path);
ImageData imgData = image.getImageData();
int intH = image.getBounds().height;
int intW = image.getBounds().width;
int h = (150 * intH) / intW;
int w = 150;
if (h > 150){
h = 150;
w = (150 * intW) / intH;
}
imgData = imgData.scaledTo(w, h);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { imgData };
imageLoader.save(Variables.getStrResources()
+ "Pics\\" + a.getHerd_id() + ".jpg",
SWT.IMAGE_JPEG);
try {
Frm_Animal.setAnimalEditSC(Frm_Animal
.createAnimalComp(a));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
GridData gd_btnSave = new GridData(SWT.LEFT, SWT.CENTER, false, false,
1, 1);
gd_btnSave.widthHint = 60;
btnSave.setLayoutData(gd_btnSave);
btnSave.setText("Save");
---Code That Creates Button In Composite---
Button btnPic = new Button(composite, SWT.CENTER);
btnPic.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
Comp_Add_Photo photo = new Comp_Add_Photo(
scrolledComposite, SWT.FILL, a);
setAnimalEditSC(photo);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
Image image = null;
String strPic = Variables.getStrResources() + "Pics\\" + a.getHerd_id() +".jpg";
if(CC_Files.fileExists(strPic)){
image = (Image) SWTResourceManager
.getImage(strPic);
}else {
image = (Image) SWTResourceManager
.getImage(Variables.getStrResources() + "black_cow.png");
}
btnPic.setImage(image);
btnPic.setToolTipText("Click Here To Add Photo");
GridData gd_btnPic = new GridData(SWT.CENTER, SWT.CENTER, false, false,
1, 7);
gd_btnPic.heightHint = 160;
gd_btnPic.widthHint = 160;
btnPic.setLayoutData(gd_btnPic);
Upvotes: 3
Views: 553
Reputation: 3241
I think it is probably because you are using SWTResourceManager
and it must have a cache of some kind. Since the path of the image remains same so it returns the older image. Which API does SWTResourceManager
belong to? Can it dispose a single image before you reload the new image? Perhaps call something like SWTResourceManager#dipose()
. I am hoping there would be some way to just clear the cache for specific images.
Upvotes: 4