Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

JComboBox and ActionEvent

Thread view: 
Morten Alver - 14 Feb 2005 09:45 GMT
Hi,

I'm using an editable JComboBox to let the user select a word, which is
added to a list when the user selects. The JComboBox sends an
ActionEvent when 1) the user edits and presses ENTER, 2) The user
selects from the dropdown using the mouse or clicking ENTER and 3) When
the user navigates the dropdown with arrow keys. I need to ignore 3),
since I only want to add the word when the user has selected, not during
navigation. The only way I found was by looking at the getModifiers() in
ActionEvent, which is 0 for the navigation and 16 for the actual
selection. The edit action has a different action command, so I can
discern all three variants.

The JavaDocs say that getModifiers() "Returns the modifier keys held
down during this action event.", which seems to have no relevance to my
issue. Still, this seems to work. But why are the modifiers different?
Is this well defined behaviour that I can trust everywhere, or did it
work "by accident"?

--
Morten
John McGrath - 14 Feb 2005 19:04 GMT
> The only way I found was by looking at the getModifiers() in ActionEvent,
> which is 0 for the navigation and 16 for the actual selection. The edit
[quoted text clipped - 6 lines]
> Is this well defined behaviour that I can trust everywhere, or did it
> work "by accident"?

I would say it is "by accident".  The modifier value of 16 is the same as
InputEvent.BUTTON1_MASK, which just indicates that Mouse Button 1 was
pressed.  You really cannot count on that always being the case.  For
example, the user could use a different mouse button, or could hold down
the Control, Shift, or Alt keys when he makes the selection, which would
result in a different modifier value.  Also, how the selection can be made
is look-and-feel dependent, so it is possible that some other key could be
used.  Add assistive technologies into the mix (voice commands and other
specialized devices), and you really cannot know how the selection will be
made.

The problem here is that, despite the fact that JComboBox provides support
for ActionListeners, it really supports a selection paradigm rather than
an "action" paradigm.  It provides gestures to change the choice, but not
one that indicates a final choice.  Nothing says "I've made my choice, so
go do it", whatever "it" is.

The Swing designers certainly could have designed the JComboBox that way,
but they did not do so.  Grafting this behavior onto a combo box, using
the existing actions will cause problems.

The component that best supports actions is the JButton, so perhaps it
would be better to place an "Add Word" button next to the combo box that
actually adds the word.  You could also provide a hot key to perform the
add word function.

Signature

Regards,

John McGrath

Morten Alver - 15 Feb 2005 10:17 GMT
> I would say it is "by accident".  The modifier value of 16 is the same as
> InputEvent.BUTTON1_MASK, which just indicates that Mouse Button 1 was
[quoted text clipped - 6 lines]
> specialized devices), and you really cannot know how the selection will be
> made.

I see your point.

> The problem here is that, despite the fact that JComboBox provides support
> for ActionListeners, it really supports a selection paradigm rather than
> an "action" paradigm.  It provides gestures to change the choice, but not
> one that indicates a final choice.  Nothing says "I've made my choice, so
> go do it", whatever "it" is.

You're right. I actually have an "Add" button already, but I believed it
would be more convenient to avoid having to click it each time. When I
think of it now, this design decision by the Swing designers is probably
a sign that I should avoid using the combobox in this way.

Thank you for your answer, it's been very helpful!

--
Morten


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.