سؤال

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?

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top