Results 1 to 6 of 6
  1. #1
    alexapps is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Action listener is iterating multiple times when clicked

    Hi,

    Im new to these forums so please take it easy on me :) .

    Anyhow im currently writting an application for my university course that handles a fleet of vehicles and employees. This program was originally written to be used with just the console as input but Ive now adapted it to work with a GUI created with WindowBuilderPro.

    Ive hit a wall now, and im in need of some help...

    Basically when a button named delete vehicle is clicked a text field and submit button is enabled for the user to enter the vehicle registration to delete, and the submit button to call the delete function from a different class.

    Now this works fine for the first delete. All works..

    The problem arises when the user tries to delete another vehicle, the vehicle gets deleted, but the action listener runs a second time after the delete. Then causing a error because the registration that was just entered cant be found in the array.

    This problem scales with the amount of vehicles the try and delete. For example if they try and delete the third vehicle in succession, the vehicle gets deleted but the action listener iterates a further 2 times.

    Below is the code :

    Java Code:
    		//Action listener for the delete vehicle button
    		
    		btnDeleteVehicle.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mouseClicked(MouseEvent arg0) {
    		
    				txtRegistration.setEnabled(true);
    				btnSubmit.setEnabled(true);
    	
    				// Action listener for the submit button, embedded within the Delete vehicle function
    				btnSubmit.addActionListener(new ActionListener() {
    					public void actionPerformed(ActionEvent arg0) {
    						
    						Boolean result;
    						result = theFleet.deleteVehicle(txtRegistration.getText());
    						
    								if (result == true){ //Return of true, vehicle has been deleted
    									
    								
    									textOutput.setText("Vehicle has been deleted");
    									textOutput.setBackground(Color.green);
    									
    								}
    								
    								else if (result == false){
    									
    									
    									textOutput.setText("Vehicle not deleted. \n Either vehicle is on rent, \n or it cannot be found");
    									textOutput.setBackground(Color.red);
    									
    								}
    						
    						txtRegistration.setText("");
    						txtRegistration.setEnabled(false);
    						btnSubmit.setEnabled(false);
    								}
    				});
    					}			
    		});

    I cant see anything wrong with this, but im new to GUI design and action listeners as such.

    Any help is appreciated


    Alex

    * edit * If you need the full GUI class code posted, please let me know. Its rather lengthy though
    Last edited by alexapps; 03-02-2011 at 09:24 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default

    With every mouseClicked(...), you're adding another ActionListener.
    Java Code:
    public void mouseClicked(MouseEvent arg0) {
      txtRegistration.setEnabled(true);
      btnSubmit.setEnabled(true);
      // Action listener for the submit button, embedded within the Delete vehicle function
      btnSubmit.addActionListener(new ActionListener() {
    Also, why do you have a MouseListener added to btnDeleteVehicle and not an ActionListener?

    db

  3. #3
    alexapps is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Ah ok, so to solve this problem I would write the action listener for the submit button outside of this action listener. And then call it from with inside the delete action listener?

    And as for the mouseClicked, as you posted I just realised that I hadn't changed it back while fiddling..

    Just one final quick question, Is it possible to create multiple action listeners for one submit button? Maybe a silly question but im still learning.

    Thanks Alex

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by alexapps View Post
    Just one final quick question, Is it possible to create multiple action listeners for one submit button? Maybe a silly question but im still learning.
    Yes, a button can have as many listeners as you desire. One caveat though -- there's no guarantee on the order that they will fire.

  5. #5
    alexapps is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    ok, so following that is it not practical then to have one button that has many uses?

    for example my submit button is disabled untill the user selects what they want to do, i.e. delete, add, loan.

    Once the users selects the button is enabled along with relevant text fields for input. Initianally i though one button would be sufficient, but im starting to think not.

    see the screenshot below, as Im finding it hard to explain.

    Thanks for the help by the way

    Alex


  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    One "submit" button is fine. I'd use a group of JRadioButtons for your loan/return/delete/add logic. Have them all added to a ButtonGroup so only one can be active at a time, give them an ActionListener that activates the submit button when any one of them is selected, and then query the state of these JRadioButtons from the ActionListener added to the submit button.

Similar Threads

  1. Replies: 4
    Last Post: 02-23-2011, 10:25 PM
  2. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 06:39 PM
  3. My buttons all perform their action like 100 times
    By 711groove in forum New To Java
    Replies: 0
    Last Post: 12-13-2009, 11:49 AM
  4. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 AM
  5. calling action class multiple times
    By sindhu_shiva in forum Web Frameworks
    Replies: 0
    Last Post: 08-07-2009, 03:44 PM

Posting Permissions

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