Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ComboBox ItemListener calling twice..

 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Whenever a item is selected from JComboBox, the itemListener
method is called twice..... !!???

Why is it happening, i need this method to be called only once with
the current selected item. But its calling this method with both, current
and previous selected item...

here is the code...

 
sasi kala
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi vijay,

The behavior of ItemListener is like that only.
It will call itemStateChanged two times because,
one of the item lost focus,
another one gains focus.
so it will call two times.

My suggestion is use ActionListener , and write actionPerformed like this


Hope This Helps.
All The Best
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<aircode>

 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked!

Thanks.....
 
Reyada Wolak
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sasi kala wrote:Hi vijay,

The behavior of ItemListener is like that only.
It will call itemStateChanged two times because,
one of the item lost focus,
another one gains focus.
so it will call two times.

My suggestion is use ActionListener , and write actionPerformed like this


Hope This Helps.
All The Best


i tried to setFocusable(false) and it works just fine.
 
Darryl Burke
Bartender
Posts: 5147
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you missed the point, and also that this question was asked and resolved 5 years ago.

sasi kala's terminology was off. One item loses the selection, and the other gains it. Not focus.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic