Solved

JComboBox Item Selection Problem

Posted on 2007-12-05
11
1,162 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

705 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

16 Experts available now in Live!

Get 1:1 Help Now