// The following few lines are used to register esc to close the dialog
Action cancelKeyAction = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
XDialog.this.dispose();
}
};
KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke((char)KeyEvent.VK_ESCAPE);
InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getRootPane().getActionMap();
if(inputMap != null && actionMap != null){
inputMap.put(cancelKeyStroke, "cancel");
actionMap.put("cancel", cancelKeyAction);
}
The best teams have no specialists, only general contributors with special skills
public class XDialog extends JDialog
{
/////////////
// All the constructors go here
// and all the constructors after calling super's version calls initXDialog method
/////////////
// -- static block
{
// Add Escape key binding to the shared JTextComponent key bindings so that if any
// text component is inside Dialog then dispose that dialog
KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke((char)KeyEvent.VK_ESCAPE);
Keymap map = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP);
map.addActionForKeyStroke(cancelKeyStroke, cancelKeyAction);
}
private static Action cancelKeyAction = new AbstractAction()
{
public void actionPerformed(ActionEvent ae)
{
Component comp = (Component) ae.getSource();
Window window = SwingUtilities.windowForComponent(comp);
if(window instanceof Dialog){
window.dispose();
}
else if(comp instanceof JTextComponent && ! (comp instanceof JFormattedTextField)){
JTextComponent tc = (JTextComponent) comp;
int end = tc.getSelectionEnd();
if(tc.getSelectionStart() != end){
tc.setCaretPosition(end);
}
}
}
};
/**
* Method for initialization.
* This implementation adds a window listener to dispose itself while closing
* This also adds functionality of disposing if "ESCAPE" is pressed
*/
private void initXDialog()
{
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke((char)KeyEvent.VK_ESCAPE);
InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getRootPane().getActionMap();
if(inputMap != null && actionMap != null){
inputMap.put(cancelKeyStroke, "cancel");
actionMap.put("cancel", cancelKeyAction);
}
}
} // end of class XDialog
KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke((char)KeyEvent.VK_ESCAPE);
JTextComponent.KeyBinding[] bindings = new JTextComponent.KeyBinding[]{ new JTextComponent.KeyBinding(cancelKeyStroke, "reset-field-edit") };
JTextComponent.loadKeymap(getKeymap(), bindings, getActions());
The best teams have no specialists, only general contributors with special skills
The best teams have no specialists, only general contributors with special skills
The best teams have no specialists, only general contributors with special skills
There's no need to fight though
The best teams have no specialists, only general contributors with special skills
Don't get me started about those stupid light bulbs. |