-2

How to Create hot keys for form which is made by java swing?for example the form is create for student details means if I press ALT+N means the cursor will goto that name entering field.ALT+R means the cursor will goto Reg.no entering Field.same like as mark1(m1),mark(2) and so on.At that same time the form contains save,exit buttons if I press CTRL+S means the Save button will select.CTRL+X means the exit button will select.how to do this?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
java872
  • 89
  • 1
  • 3
  • 5

3 Answers3

3

See How to Use Key Bindings, then use that knowledge in conjunction with requestFocusInWindow().


CTRL+X means the exit button will select.

See also setMnemonic(char) for buttons and setAccelerator(KeyStroke) for menu items. Or more generally, constructing those controls using an Action that has the values configured.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

Please refer the following link

http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html It might be a good place to start.

sasankad
  • 3,543
  • 3
  • 24
  • 31
2

means if I press ALT+N means the cursor will goto that name entering field

This is generally done by using JLabel/JTextField pairs. Something like:

JLabel firstNameLabel = new JLabel("First Name");
JTextField firstNameTextField(15);
firstNameLabel.setLabelFor( firstNameTextField );
firstNameLabel.setDisplayedMnemonic( 'F' );
panel.add( firstNameLabel );
panel.add( firstNameTextField );

Then using Alt-F will set focus on the text field.

The Key Bindings will be done automatically for you.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks! My learn item for the day. I don't know how I could have missed that method for associating a label with an input control. Brilliant. I'm busy playing with it now. :-) – Andrew Thompson Dec 21 '11 at 05:10