Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You seem to be passing null handlers into your GUI. Where for instance do you instantiate the inputHandler variable before using it? </p> <p>For example:</p> <pre><code>public class DrawPanel extends JPanel { //... InputHandler inputHandler; // here you declare the variable //... public DrawPanel () { setIgnoreRepaint(true); // why this line? setVisible(true); // not needed in a JPanel's code setFocusable(true); addKeyListener(inputHandler); // here you use a null variable addMouseListener(inputHandler); // ditto, here you use a null variable new InputHandler(); // I don't know what you're doing here } </code></pre> <p>In the code above I don't see anywhere you have <code>inputHandler = new InputHandler()</code> before using it. Your line where you appear to create a new InputHandler, you don't assign the object to any variable or use it, and so it seems a futile line of code, hence my comment on how I'm not sure what that line is supposed to achieve. Note that these problems have nothing to do with Swing and all to do with basic core Java.</p> <p>Also: </p> <ul> <li>Your handler classes should not extend Swing components such as JPanels. They should implement listener interfaces only.</li> <li>You should avoid use of KeyListeners with Swing GUI's and instead use Key Bindings if possible. Please Google for and check the Key Bindings tutorial for more on this.</li> <li>The use of handlers is well explained in the Oracle Swing tutorials. Again, please Google these and study them. I can attest to their usefulness as they are where I learned my Swing coding.</li> </ul>
 

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