I was demonstrating my program by basically mashing keys on the number pad to get some dummy data in there really quickly, and discovered that occasionally that causes invalid input to be entered. It seems like some sort of threading issue, but my understanding of Swing events is that they basically are fired sequentially from the same thread. I am not explicitly creating any threads of my own, but I am doing a lot of custom painting using paintComponent(Graphics g)
. Most keypresses eventually trigger a repaint()
deep in the stack. Could that be causing my issue? Why? If so, any idea how to fix it? If not, any idea what else it could be?
Also, does anyone know how to make an automated test for this? The relevant event handlers are shown below.
public void keyTyped(KeyEvent e)
{
if (e.getKeyChar() == myKeyChar)
runPrimaryFunction(); // Does some calculations and triggers a repaint
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == myKeyCode)
runPrimaryFunction();
}
/********* Another class listening to the same JFrame ********/
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_UP)
{
scrollUp();
repaint();
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
scrollDown();
repaint();
}
}