首页 \ 问答 \ Java SWT按钮图像不刷新(Java SWT Button Image Not Refreshing)

Java SWT按钮图像不刷新(Java SWT Button Image Not Refreshing)

我在按钮中显示图像,当单击按钮时,它会打开一个复合图像,允许我选择另一张图片用于按钮。 执行保存操作后,下面的代码会调整图片大小并将其保存到目录中。 然后它加载重新加载复合材料。 当复合加载按钮时,根据用户正在查看的记录的ID从默认位置拉取其图像。 我遇到的问题是按钮上的图像保持不变,除非我关闭并重新加载应用程序。 需要注意的一件有趣的事情是,当加载默认按钮图像时(即没有为其id保存图片),按钮图像会按原样改变,但仅在第一次时改变。 希望我已经说清楚我的问题,如果你需要我澄清请评论。

---保存按钮代码 -

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");

---创建复合按钮的代码---

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);

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);

原文:https://stackoverflow.com/questions/13962641
更新时间:2022-05-19 22:05

最满意答案

你没有调用正确的方法。 你需要调用em.createQuery


You're not calling the right method. You need to call em.createQuery.

相关问答

更多

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)