Results 1 to 6 of 6
- 03-02-2011, 08:15 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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 thoughLast edited by alexapps; 03-02-2011 at 08:24 PM.
- 03-02-2011, 08:41 PM #2
With every mouseClicked(...), you're adding another ActionListener.
Also, why do you have a MouseListener added to btnDeleteVehicle and not an 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() {
db
- 03-02-2011, 08:48 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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
-
- 03-02-2011, 09:24 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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
-
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
-
How to add mouse clicked listener on rows of a jtable
By pink123 in forum AWT / SwingReplies: 4Last Post: 02-23-2011, 09:25 PM -
Action Listener
By greatmajestics in forum AWT / SwingReplies: 8Last Post: 03-25-2010, 05:39 PM -
My buttons all perform their action like 100 times
By 711groove in forum New To JavaReplies: 0Last Post: 12-13-2009, 10:49 AM -
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 08:51 AM -
calling action class multiple times
By sindhu_shiva in forum Web FrameworksReplies: 0Last Post: 08-07-2009, 02:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks