Blanket Truth

امیرحسین لطیفی - ۱۵ شهریور ۱۳۸۷

Truth is like a blanket that always leaves your feet cold.
You push it, stretch it, it’ll never be enough. You kick at it, beat it, it’ll never cover any of us. From the moment we enter crying to, to the moment we leave dying, it’ll just cover your face as you wail and cry… and scream.

from Dead Poets Society (1989) movie.


How to add KeyListener to JDialog

امیرحسین لطیفی - ۱۲ دی ۱۳۸۶

You can add KeyListener to your JDialog component as you can add it to any other swing components but when you add other components to JDialog, they eat key events.
To solve this problem you should register KeyboardAction for JDialog. In below code snippet typically I want sense F1 key pressing to do something:

ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
};
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
JRootPane rootPane = getRootPane();
rootPane.registerKeyboardAction(actionListener, keyStroke,JComponent.WHEN_IN_FOCUSED_WINDOW);