// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());

    JTable table = new JTable();
    URL url = getClass().getResource("restore_to_background_color.gif");
    Object[][] data = {
        {"Default ImageIcon", new ImageIcon(url)},
        {"ImageIcon#setImageObserver", makeImageIcon(url, table, 1, 1)}
    };
    String[] columnNames = {"String", "ImageIcon"};
    table.setModel(new DefaultTableModel(data, columnNames) {
      @Override public Class<?> getColumnClass(int column) {
        return getValueAt(0, column).getClass();
      }

      @Override public boolean isCellEditable(int row, int column) {
        return column == 0;
      }
    });
    table.setAutoCreateRowSorter(true);
    table.setRowHeight(20);

    add(new JScrollPane(table));
    setPreferredSize(new Dimension(320, 240));
  }

  private static ImageIcon makeImageIcon(URL url, JTable table, int row, int col) {
    ImageIcon icon = new ImageIcon(url);
    // Wastefulness: icon.setImageObserver((ImageObserver) table);
    icon.setImageObserver((img, infoflags, x, y, w, h) -> {
      // @see http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
      if (!table.isShowing()) {
        return false; // @see javax.swing.JLabel#imageUpdate(...)
      }
      if ((infoflags & (FRAMEBITS | ALLBITS)) != 0) { // @see java.awt.Component#imageUpdate(...)
        int vr = table.convertRowIndexToView(row); // JDK 1.6.0
        int vc = table.convertColumnIndexToView(col);
        table.repaint(table.getCellRect(vr, vc, false));
      }
      return (infoflags & (ALLBITS | ABORT)) == 0;
    });
    return icon;
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

@@ -134,16 +134,69 @@
 et.URL;%0A
+import java.util.Objects;%0Aimport java.util.Optional;%0A
 import j
@@ -355,17 +355,16 @@
 out());%0A
-%0A
     JTab
@@ -396,28 +396,89 @@
     
-URL url = getClass()
+ClassLoader cl = Thread.currentThread().getContextClassLoader();%0A    URL url = cl
 .get
@@ -487,16 +487,24 @@
 source(%22
+example/
 restore_
@@ -526,24 +526,157 @@
 olor.gif%22);%0A
+    Icon icon = Optional.ofNullable(url).%3CIcon%3Emap(ImageIcon::new)%0A        .orElseGet(() -%3E UIManager.getIcon(%22html.missingImage%22));%0A
     Object%5B%5D
@@ -717,34 +717,20 @@
 eIcon%22, 
-new ImageIcon(url)
+icon
 %7D,%0A     
@@ -1306,30 +1306,29 @@
   %7D%0A%0A  p
-rivate
+ublic
  static 
 ImageIco
@@ -1319,21 +1319,16 @@
  static 
-Image
 Icon mak
@@ -1377,24 +1377,58 @@
  int col) %7B%0A
+    if (Objects.nonNull(url)) %7B%0A  
     ImageIco
@@ -1452,24 +1452,26 @@
 eIcon(url);%0A
+  
     // Waste
@@ -1529,16 +1529,18 @@
 e);%0A    
+  
 icon.set
@@ -1587,16 +1587,18 @@
 h) -%3E %7B%0A
+  
       //
@@ -1670,24 +1670,26 @@
 .html%0A      
+  
 if (!table.i
@@ -1698,24 +1698,26 @@
 howing()) %7B%0A
+  
         retu
@@ -1772,26 +1772,82 @@
 (...)%0A      
-%7D%0A
+  %7D%0A        // @see java.awt.Component#imageUpdate(...)%0A  
       if ((i
@@ -1891,53 +1891,11 @@
 0) %7B
- // @see java.awt.Component#imageUpdate(...)%0A
+%0A  
     
@@ -1954,16 +1954,18 @@
 K 1.6.0%0A
+  
         
@@ -2014,24 +2014,26 @@
 l);%0A        
+  
 table.repain
@@ -2065,24 +2065,26 @@
 c, false));%0A
+  
       %7D%0A    
@@ -2081,24 +2081,26 @@
     %7D%0A      
+  
 return (info
@@ -2132,28 +2132,32 @@
 ) == 0;%0A    
+ 
+ 
 %7D);%0A
+  
     return i
@@ -2161,16 +2161,88 @@
 n icon;%0A
+    %7D else %7B%0A      return UIManager.getIcon(%22html.missingImage%22);%0A    %7D%0A
   %7D%0A%0A  p

argument 'getClass().getResource(...)' might be null