Note that there are some explanatory texts on larger screens.

plurals
  1. POcapture key press within frame
    text
    copied!<p>This seems like a simple behavior, but I'm having difficulty making it happen. I'm working on software which graphs data. I want to redraw the graph when the user hits enter. Well more accurately I want to draw the graph when the user hits enter and doesn't have a text field selected; but for now I'll be satisfied with drawing whenever the user hits enter.</p> <p>I tried installing a basic <code>KeyListener</code> to the frame first, but that doesn't work since the <code>JFrame</code> children, not the frame, receive the event.</p> <p>I then tried to use the <code>KeyEventDispatcher</code>, but it's proving too global a concept. I can have two graphs on screen at once, and an old graph can be closed or replaced with a new graph. With the <code>KeyEventDispatcher</code> I have no easy way of knowing which plot I want to activate out of the multiple plots open at a time and the plots currently open now may not be the plots that were opened when I instantiated the key dispatcher. I know this solution could still work, but it requires my storing extra data as to what plot is currently active that doesn't fit well into the program architecture.</p> <p>It seems as if there should be an easier method to capture any <code>KeyEvents</code> dispatched to a <code>JFrame/JPanel</code> or any of the <code>JFrame's</code> children; but ignore events dispatched to other frames. Preferable a method that can be added to the frame and thus is automatically disposed when the frame is disposed. Can anyone suggest a simpler method then what I've tried?</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