1

In Swing we use a label control to indicate the user what information is requested in -generally- the following "entry" control. This entry control can be an entry, listbox, password etc.

We can inform a displayedMnemonic to instruct the user that holding down Alt key together with an accelerator key will select the associated entry control.

This is how I am designing a form, but when I press Alt+A, for example, the "associated" control doesn't get selected, sure, because I don't know how to bind the two controls using the IDE (Netbeans in this case).

I know I can trap the event generated by the label control and programmatically select whatever next control I need, but really do I? Isn't there an "implicit" way to make the binding/coupling between the two controls?

As an example, I work with another programming language, Clarion for Windows. In Clarion we can define a complex data structure of the type Window, like so:

MyWindow     WINDOW( 'Test' ), GRAY, DOUBLE, CENTER
               PROMPT( '&File Name:' ), AT( 10, 10 )
               ENTRY( @S127 ), USE( szFileName ), AT( 10, 20 )
               PROMPT( '&Status:' ), AT( 50, 10 )
               LIST, FROM( 'Open|#0|Closed|#1' ), USE( ?lstStatus ), AT( 50, 20, 100, 20 )
             END 

In the above example, the PROMPT control is implicitly bound to the ENTRY control because it appears immediatelly before the later, and the same happens between the "Status" and "?lstStatus" controls.

The RTL takes care of handling Alt+F to make the selection of the szFileName control, generating the events etc. No need to say that this is a major time saving feature for the programmer, as he/she doesn't need to program every selection for every prompt that appears on the window.

This is what I'm looking for in Swing. Any ideas?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Gustavo Pinsard
  • 267
  • 3
  • 10

1 Answers1

4

You'll want to use JLabel.setLabelFor

This will associate the label with the specified control & allow you to select the control using the labels short cuts

kleopatra
  • 51,061
  • 28
  • 99
  • 211
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • To which method are you referring. I am not familiar with it and wanted to read the documentation, but the link points simply to the `Component` class and a search on that name gives no results – Robin Aug 28 '12 at 20:25
  • Thanks, Mad. With your answer I was able to spot the "labelFor" field in the properties panel for the label, in Netbeans. – Gustavo Pinsard Aug 28 '12 at 20:29
  • @Robin thanks for the check, fat fingers on the iPad, first thing in the morning, never a great combination – MadProgrammer Aug 28 '12 at 20:35
  • +1 Thanks for the correction. Never used that method, but seems interesting – Robin Aug 28 '12 at 20:35