Creating hotkeys for java swing form

How to create hotkeys for a form that are created using java swing? For example, a form is created for student information, which means that if I press ALT + N, the cursor will accept this name, which introduces the field. ALT + R means that the cursor will be goto Reg.no enter Field.same as mark1 (m1), mark (2), etc. At the same time, the form contains save, exit buttons, if I press CTRL + S, it means that the Save button will select. CTRL + X means that the exit button will select. How to do it?

-2
source share
3 answers

See How to use key bindings , then use this knowledge in conjunction with requestFocusInWindow() .


CTRL + X means that the exit button will select.

See. Also setMnemonic(char) for the buttons and setAccelerator(KeyStroke) for menu items. Or, more generally, building these controls with an Action that has customized values.

+3
source

Please refer to the following link

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

+2
source

means that if I press ALT + N, the cursor will take this name, introducing the field

This is usually done using JLabel / JTextField pairs. Sort of:

 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, the focus on the text field will be set.

Key bindings will be done automatically for you.

+2
source

Source: https://habr.com/ru/post/1387358/


All Articles