• Post Reply Bookmark Topic Watch Topic
  • New Topic

how can you setselecteditem in jcombobox without triggering itemstatechanged?

 
mark goking
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried to set the jcombobox's comboboxmodel but it still gets called. is this the wrong way?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the source code JComboBox#setSelectedItem



Looks like, it is behaving like expected. Why do you want it to not fire the event?
One workaround could be remove the listener, change the selection, add the listener.
 
mark goking
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because on startup i want to set a default selected item without firing any events like

jcombobox.getModel().setSelectedItem(obj);
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always add the listener after you have set up the combo box with the selected item.
 
mark goking
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi maneesh, yes you are right.
i was looking for a way not to do that kind of thing. what i did was add a variable inside the itemstatechanged instead so it wouldnt get executed
 
santhosh varala kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark chester goking wrote:hi maneesh, yes you are right.
i was looking for a way not to do that kind of thing. what i did was add a variable inside the itemstatechanged instead so it wouldnt get executed


Basically their are ways we can do it.

1. Playing around with addListener and remove listener while updating the selected item.

2. As you said you can use a variable as to process the code after firing the event. Its purely depends on flag settings.(this becomes a patch work).

3.You can override the firing the events, when updating is done (whether to fire or not). This can be done custommodels which implement abstract event listener.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!