Results 1 to 5 of 5
  1. #1
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default Not trigger Action when adding to JComboBox

    I've searched and searched the internet and I cannot find a definitive answer to this question.

    Is the only way to add to a CB without firing the actionlistener to either disable it first or to use an If statement with the action.getValue()? I would think there would be a better way, maybe in which I can set the default for action listeners to not trigger when adding to any of the comboboxes.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Could u please explain ur problem more clearly.
    If u don't want ur combobox to fire during adding take out the action listener.
    Ramya:cool:

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    I usually have a flag that indicates 'background actions' like this:
    Java Code:
    boolean updatingGui = false:
    
    p. v. somemethod(){
       updatingGui = true:
       // Add to Comboboxes
       updatingGui = false:
    }
    
    p. v. actionPerformed(AE e){
       if(updatingGui ){
          return;
       }
       // whatever
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    If u don't want ur combobox to fire during adding take out the action listener.
    I need the ActionListener, but I only need it to fire for Selections, not adding items but I didn't want to have to Disable the listener each time I added items, seemed like a waste of resources. I make changes to the list throughout the program so it isn't just a one time thing in which I could add the listener after the initial add either.

    Quote Originally Posted by PhHein View Post
    I usually have a flag that indicates 'background actions' like this:

    Code:
    boolean updatingGui = false:

    p. v. somemethod(){
    updatingGui = true:
    // Add to Comboboxes
    updatingGui = false:
    }

    p. v. actionPerformed(AE e){
    if(updatingGui ){
    return;
    }
    // whatever
    }
    Awesome, this is a much better idea than disabling the listener, and it saves from having to use the getValue() method each time. Thanks.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    but I didn't want to have to Disable the listener each time I added items, seemed like a waste of resources.
    I don't understand that comment. I would do:

    a) comboBox.removeListener
    b) add items
    c) comboBox.addListener

    All the code is in one place and indicates your intent not to fire an event.

    I think this is cleaner than using a boolean variable which is set in one place and then tested in another place

Similar Threads

  1. generating trigger using java
    By money3 in forum JDBC
    Replies: 1
    Last Post: 10-06-2009, 12:56 PM
  2. Mutating trigger help
    By xcallmejudasx in forum JDBC
    Replies: 4
    Last Post: 04-24-2009, 11:49 PM
  3. programmaticly trigger an event
    By maxim in forum AWT / Swing
    Replies: 3
    Last Post: 02-23-2009, 04:27 PM
  4. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 11:14 AM
  5. Adding items to a jComboBox
    By tronovan in forum New To Java
    Replies: 0
    Last Post: 08-08-2007, 09:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •