简体   繁体   中英

How update an ImageIcon from JButton/JLabel

I'm having problems updating ( or refreshing) an Icon from button after closing a modal dialog. The image is basically overwritten by some actions of JDialog .

This is my code:

conf = new Configurar(this, true,control);           
conf.setVisible(true); // Open dialog
System.out.println("Cerrado"); // Check if is closed (debug)
String logo =(String)config.get("logo"); // get path from image
File newIcon =new File(logo); // Desesperate try
ImageIcon img = new ImageIcon(newIcon.getAbsolutePath()); 
btn_main_image.setIcon(img);
this.update(btn_main_image.getGraphics());
btn_main_image.updateUI(); // First Try
this.repaint(); // Second Try

The first time it works fine, but when I open the dialog and change the image remains the same.

conf = new Configurar(this, true,control);           
conf.setVisible(true); // Some kind of file chooser ??
File newIcon =new File(logo);
if (newIcon.exists()) {
   ImageIcon img = new ImageIcon(newIcon.getAbsolutePath()); 
   btn_main_image.setIcon(img);
   //this.update(btn_main_image.getGraphics()); // WHAT IS THIS?!?!?!
   //btn_main_image.updateUI(); // NO NO NO, this has nothing to do with refreshing the graphics, it's L&F stuff
   btn_main_image.invalidate();
   // Use this ONLY if invalidate doesn't work...
   btn_main_image.revalidate();
   btn_main_image.repaint();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM