-
KeyListener and JDK 1.4
Hello,
I use a JDialog and added a KeyListener like that:
Code:
this.addKeyListener(new java.awt.event.KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
this_keyPressed(e);
}
});
Under JDK1.3 I got all events from child components there. But under JDK1.4 I get no longer any event.
I tried with an KeyEventDispatcher, but I doesn't work. Must I use InputMap?
Any example code?
Thanks
Michael
-
afaicr, someone else tried this recently..
hangon while i find the thread...
-
http://www.codeguru.com/forum/showth...ht=Timer+focus is what i was thinking of.. it's not quite the same problem.. let me know if it provides any inspiration
-
Hi,
thanks a lot. I think it could work that way - but its not the best way.
Michael
-
why not? why would you want to send every event to one component? what then.. a massive if else if else if else if to decide what to do?
I know that microsoft windows works that way (basically just a massive switch loop that processes the messages in non-blocking style) but it doesnt mean it's the best way.. infact.. if you think that before IRQ came along, the CPU had to check every card and port hundreds of times a second, to see if it was needing attention, it becomes clear that "one thing that does everything" is a trend we are moving away from..
but, if you want to do it that way, you can simply implement a simple keylistener that defers all events to a single component, then use a for loop to iterate the contents of your jframe (getComponents() method of ancestor Component) and add the listener to every one of them..
-
probably something like this:
Code:
Component[] c = jframe.getContentPane().getComponents();
MyDeferringKeyListener mdkl = new MyDeferringKeyListener();
for(int i=0; i<c.length; i++){
if c.isFocusable() //non focusable probably dont emit keyevents
c.addKeyListener(mkdl);
}
it's lame though.. UI design should have a lot of effort put into it, because it is what the user interacts with.. do whatever you like that is hidden, but be consistent and sensible when working with the user
-
Hi,
I nead it e.g. for CTRL-S -> Save or ESC -> Exit the dialog.
I did it that way:
and it works fine.
Code:
public class LoginDialog extends JDialog implements KeyEventDispatcher
…
private void jbInit() throws Exception
{
KeyboardFocusManger.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
…
}
public boolean dispatchKeyEvent(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
//do something
}
return true; // true if let other eventdispatcher work with that event, false if not
}
Michael