Note that there are some explanatory texts on larger screens.

plurals
  1. POJPanel.addComponentListener does not work when the listener is a class variable
    text
    copied!<p>I have a public class which has the following method and instance variable:</p> <pre><code>public void setImagePanel(JPanel value) { imagePanel = value; if (imagePanel != null) { //method 1 : works imagePanel.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent evt) { System.out.println("Here 1"); } }); //method 2 : does not work panelResizeListener = new ResizeListener(); imagePanel.addComponentListener(panelResizeListener); //method 3 : works //ResizeListener listener = new ResizeListener(); //imagePanel.addComponentListener(listener); //method 4 : works //imagePanel.addComponentListener(new ResizeListener()); //method 5 : does not work -- THIS IS THE DESIRED CODE I WANT TO USE imagePanel.addComponentListener(panelResizeListener); } } public class ResizeListener extends ComponentAdapter { @Override public void componentResized(ComponentEvent evt) { System.out.println("RESIZE 3"); } } private ResizeListener panelResizeListener = new ResizeListener(); private static JPanel imagePanel; </code></pre> <p>Each of the methods above correspond the to code immediately below until the next //method comment. What i don't understand is why i can't use the class instance variable and add that to the <code>JPanel</code> as a component listener. </p> <p>What happens in the cases above where i say that the method does not work is that i don't get the <code>"RESIZE 3"</code> log messages. In all cases where i list that it works, then i get the <code>"RESIZE 3"</code> messages.</p> <p>The outer class is public with no other modification except that it implements an interface that i created (which has no methods or variables in common with the methods and variables listed above).</p> <p>If anyone can help me i would greatly appreciate it. This problem makes no sense to me, the code should be identical.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload