Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically assig keys to buttons?

I have a section in my GUI that is generated dynamically according to a list of objects. So, for each object in that list I want to create a JButton and associate a keyboard shortcut.

For example:

for (String tag : testTags) {
    new JButton(tag).setMnemonic(KeyEvent.VK_F1);
}

How do I make the code "setMnemonic(KeyEvent.VK_F1)" dynamic in an elegant way? Is there some way to get a range of keys automatically and then use it in this iteration?

Thanks!

like image 260
ktulinho Avatar asked Jun 17 '11 18:06

ktulinho


1 Answers

An Action is well suited for this. See How to Use Actions for more.

like image 87
trashgod Avatar answered Nov 02 '22 23:11

trashgod