Problem to override a component's existing key binding

Hi !

Just tell me something, that how to override a component's key binding?

As you know, when a text field [ I mean to say her object of the JTextField ] has the focus, pressing

Ctrl + A key would select the entire line the text field contains.

But, I want the text field to do something different; for say, pressing Ctrl + A key text field

should display a JOptionPane's message dialog box.

How can I do this ?

If there is a way, then please send a code explaining that.

[523 byte] By [Sunmembera] at [2007-10-1 14:18:00]
# 1

Hi,

You can overwrite the action in the ActionMap.

Try the following sample code.

import java.awt.* ;

import java.awt.event.* ;

import javax.swing.* ;

public class OverKB extends JFrame {

public OverKB() {

super( "Overriding existing Key Binding." ) ;

setDefaultCloseOperation( EXIT_ON_CLOSE ) ;

init() ;

setSize( 400 , 300 ) ;

setVisible( true ) ;

}

public void init() {

JTextArea ta = new JTextArea() ;

getContentPane().add( new JScrollPane( ta ) , BorderLayout.CENTER ) ;

ActionMap map = (ActionMap)UIManager.get( "TextArea.actionMap" );

map.put( "select-all" , new MyControlA() ) ;

}

public class MyControlA extends AbstractAction {

public void actionPerformed( ActionEvent actEvt ) {

JOptionPane.showMessageDialog( OverKB.this , "How is it?" , "Is it super?" , JOptionPane.INFORMATION_MESSAGE ) ;

}

}

public static void main(String[] args) {

new OverKB() ;

}

}

Please acknowledge me

LittleDanya at 2007-7-10 17:46:40 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...