Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9023049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:40:07+00:00 2026-06-16T05:40:07+00:00

I am displaying an image in a button and when the button is clicked

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:40:08+00:00Added an answer on June 16, 2026 at 5:40 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an image that when clicked, opens in a new window displaying the
I have the following image representing my button: I want to use that image
I'm having trouble with displaying image from db. I think that method for saving
I use the Remote File module for a cck field displaying a remote image.
I'm displaying lots of image cells in an UICollectionView. With one button I would
I got this issue with displaying a image that i allready have added as
I am using the below code for displaying a button as an image as
hi i have picturebox with displaying image when i click on button its fine...
I'm retrieving and displaying the list of text depending on whichever button/catagory is clicked/selected.
I am displaying image by setting the source of an Image element directly to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.