How to link a hotkey to a button in java?

I have four radio buttons and they're all part of a radioGroup. How can I link hotkeys to each of the buttons? What I want to do is link the keys '1', '2', '3' and '4' to each corresponding radiobutton.

buttonGroup1 = new javax.swing.ButtonGroup();
quizBut1 = new javax.swing.JRadioButton();
quizBut2 = new javax.swing.JRadioButton();
quizBut4 = new javax.swing.JRadioButton();
quizBut3 = new javax.swing.JRadioButton();


Use a KeyListener - you can attach them to just about any component in Swing.

What you'll probably do is attach a KeyListener to the primary JFrame in your application to capture all keypresses, and depending on which key was pressed, you will then trigger changes in the UI accordingly (e.g the selecting of a given radio button).

It's important that you attach the KeyListener to a container that will have keybaord focus pretty much all the time. You cannot, in this case, attach the KeyListener to the radio buttons themselves because the KeyListeners only see events for which they have focus. When a KeyEvent isn't absorbed by a given object, the KeyEvent is then passed on to its parent component to see if it wants to do anything with the event, and on and on all the way up to the application's window. If no KeyListener does anything with the event and you've gone all the way to the root of the component hierarchy, then nothing happens in response to the keypress and the event is essentially discarded.

Need Your Help

ajax-php works on localhost but not on realhost

php ajax json

This code works on localhost but not on real, chrome developer tools showing that value is send, and Status Code:200 OK. but that value is not reaching php page.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.