Solved

JComboBox Item Selection Problem

Posted on 2007-12-05
11
1,163 Views
Last Modified: 2013-12-29
Hello,
I have a JComboBox with items listed. User selects an Item. The Item Event is generated. It is trapped in the ItemStateChanged() listener. Now The Item is HILIGHTED on the display. Now The User Clicks on that Item AGAIN. THERE IS NO ITEM EVENT GENERATED. You must Deselect that item and select it again to regenerate the ItemEvent. This is not good for the user.

We have an application that this happens. How to solve this?. Actually, the way it works is :"The Way" ItemEvent is generated. If an Item is already selected, bu clicking on that same selected item again does not generate an ItemEvent.
0
Comment
Question by:prain
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20414797
0
 

Author Comment

by:prain
ID: 20414899
I am not sure what you explain here...

I even do not get the ItemEvent triggered when clicked in the same selected item for the second time.

public void itemStateChanged(ItemEvent iEvent)
{
  System.out.println("Passed");
}
 
I See the word "Passed" printed on my console when the item is selected for teh first time. Then when I click on the same item, there is no "Passed" printed. That tells me that the ItemEvent is not even generated at the second time.

Thanks
Kumar
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20415065
In that case, it suggests that you event queue is being blocked. Make sure you're not running anything remotely resource/time consuming in the event handler. It should be *started* there, but run in a separate thread
0
 
LVL 92

Expert Comment

by:objects
ID: 20415448
you'll need to use a MouseListener
(also try an ActionListener, it may get fired as well)
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 75 total points
ID: 20415972
Changing the listener type won't help you
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 37

Accepted Solution

by:
zzynx earned 150 total points
ID: 20417927
>> Now The User Clicks on that Item AGAIN. THERE IS NO ITEM EVENT GENERATED
>> Actually, the way it works is :"The Way" ItemEvent is generated.
>> If an Item is already selected, bu clicking on that same selected item again does not generate an ItemEvent.
That's how I would expect it to work. (And it does)

When you click on the selected item, why should ItemStateChanged() be triggered again?
The item's state does not change, so...

>> You must Deselect that item and select it again to regenerate the ItemEvent.
Completely logical. This really changes the item's state

>> This is not good for the user.
Why not? What would you want to "do" if the user selects an already selected item?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 25 total points
ID: 20417998
we know thats the way that listener works, which is why you need to use a different type of listener to get the desired behaviour as has already been suggested.

> Why not? What would you want to "do" if the user selects an already selected item?

there can be many reasons.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20418144
Sorry - prain - misread your question. That's how ItemListener is meant to work. If you want it otherwise, then changing the type of listener is essential
0
 

Author Comment

by:prain
ID: 20449317
OK. Ok. Ok. All right. Thanks all for everyone's opinion. In fact ItemEvent will not be generated when an item (which is already)selected is clicked again. That's because THERE IS NO STATE CHANGE happen. In fact when a new item is selected then the ItemEvent is generated twice (One when leaving the previous one - there is a state change and the other is when the new one get selected - again the item state change happen).

So mine is now working very well aftr I removed the ItemListener from the combobox and added a ActionListener instead.

Well I am going to split the points, so everyone will be happy. All of you have helped me previously with valuable infomation.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 20463040
Thanks 4 axxepting
0
 

Author Comment

by:prain
ID: 20464017
Good work.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 31
Error trying to install RTMT Win7 5 41
nextBoolean(double p) for Random class 3 35
Not seen Link button 5 15
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now