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
最满意答案
你没有调用正确的方法。 你需要调用em.createQuery 。
You're not calling the right method. You need to call em.createQuery.
相关问答
更多-
是。 您需要使用try/catch块,但不需要捕获Exception 。 根据API ,如果没有结果,它将抛出NoResultException ,并且由您决定如何处理它。 DrawUnusedBalance drawUnusedBalance = null; try{ drawUnusedBalance = (DrawUnusedBalance)query.getSingleResult() catch (NoResultException nre){ //Ignore this because as p ...
-
找不到命名查询(Named query not found)[2023-04-19]
你没有调用正确的方法。 你需要调用em.createQuery 。 You're not calling the right method. You need to call em.createQuery. -
EJBTransactionRolledbackException:找不到查询的实体(EJBTransactionRolledbackException: No entity found for query)[2022-05-14]
NoResultException本身不会导致事务回滚,但是如果从事务方法(在您的情况下为getLatestPatchForPackage()中抛出任何已检查的异常,则EJB将回滚事务。 因此,如果要忽略此异常,则需要在getLatestPatchForPackage()捕获它并生成适当的结果(例如,返回null或抛出应用程序异常 )。 NoResultException doesn't cause transaction rollback on its own, but EJB rolls back th ... -
SQLite外部查询返回内部查询中找不到的结果(SQLite outer query is returning results not found in inner query)[2023-09-20]
我可以验证它也发生在Firefox的SQLite插件上。 如果有任何安慰,这种形式的作品: SELECT DISTINCT "number" FROM (SELECT "letter", "number" FROM "test" ORDER BY "letter", "number") AS "test" ORDER BY "letter" LIMIT 1; 我相信SQLite规范会忽略内部查询中的LIMIT子句并将其迁移到外部。 没有限制: SELECT DISTINCT "number" FROM ( ... -
单个查询“如果找不到列值,则INSERT INTO表”(Single query for “INSERT INTO table if column value not found”)[2021-08-19]
您只需要添加FROM DUAL INSERT INTO friends (memberid) SELECT 1 FROM dual WHERE NOT EXISTS (SELECT memberid FROM friends WHERE memberid = 1) sql小提琴 You just need to add FROM DUAL INSERT INTO friends ... -
您的查询缺少括号和values关键字。 尝试 String query = ("insert into my_table (abc_id, dup_id,type_code) "+ "values (abc_seq.nextval, 2,:type)"); Query myQuery = em.createNativeQuery(query); nativeQuery.setParameter("type", code); Your query lacks a pa ...
-
获取错误未找到命名查询:UnescalatedDeadlines(Getting error Named query not found: UnescalatedDeadlines)[2023-11-28]
最好直接引用你正在使用的orm文件,以确保它们被拾取。 例如,在您的persistence.xml中,您可以添加以下内容:META-INF/Taskorm.xml It's probably best to add direct references to the orm files you're using, to make sure they are being picked up. For example, in your persiste ... -
尝试 req.setParameter("login", login); Try with req.setParameter("login", login);
-
SELECT p.ID, p.Price, COALESCE(pt.Name, p.Name) Name, COALESCE(pt.Description, p.Description) Description FROM Product p INNER JOIN User u on u.ID = @MyUserID LEFT JOIN Product_Translations pt ON pt.PID = p.ID AND pt.LANG = u.LANG SELECT p.ID ...
-
使用递归的快速解决方案: function getChildIds($id, &$count) { // Note that $count is passed in as a reference (the & before the $count) ++$count; echo $id; $sql_query="select id from rev_r_clients WHERE parent_client_id='$id'"; $res = mysql_query($sql_ ...