|
-
May 10th, 2004, 09:23 AM
#1
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
-
May 10th, 2004, 09:30 AM
#2
afaicr, someone else tried this recently..
hangon while i find the thread...
-
May 10th, 2004, 09:33 AM
#3
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
-
May 10th, 2004, 09:39 AM
#4
Hi,
thanks a lot. I think it could work that way - but its not the best way.
Michael
-
May 11th, 2004, 02:55 AM
#5
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..
-
May 11th, 2004, 03:00 AM
#6
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
-
May 14th, 2004, 09:56 AM
#7
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|