Results 1 to 3 of 3
  1. #1
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    32
    Rep Power
    0

    Default How to remove ActionListener from all of the buttons at the end

    Hello all
    How can I remove the ActionListener from the buttons of my application after i have got something happen.
    Please consider the following application:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Exer1218659 extends JFrame {
    	private String[] textButtons;
    	private Container contents;
    	private JButton[] buttons;
    	private int randomWinner;
    	private ActionHandler ah;
    
    	// constructor
    	public Exer1218659() {
    		super("grid of buttons");
    		randomWinner = 0 + (int) (Math.random() * 23);
    		contents = getContentPane();
    		contents.setLayout(new GridLayout(4, 6));
    		ActionHandler ah = new ActionHandler();
    		textButtons = new String[24];
    		buttons = new JButton[24];
    		for (int i = 0; i < buttons.length; i++) {
    			textButtons[i] = "" + i;
    			buttons[i] = new JButton(textButtons[i]);
    			buttons[i].addActionListener(ah);
    			contents.add(buttons[i]);
    		}
    		setSize(400, 600);
    		setVisible(true);
    	}
    
    	// ActionHandler class definition
    	private class ActionHandler implements ActionListener {
    		public void actionPerformed(ActionEvent ae) {
    			boolean b = false;
    			for (int i = 0; i < buttons.length; i++) {
    				if (ae.getSource() == buttons[i]) {
    					if (i == randomWinner) {
    						buttons[i].setText("Won");
    						removeActionHandler();
    					} else {
    						buttons[i].setText("No");
    					}
    				}
    			}
    
    		}
    	}
    
    	// method to remove the HctionHandler from all of the buttons
    	public void removeActionHandler() {
    		for (int j = 0; j < buttons.length; j++) {
    			buttons[j].removeActionListener(ah);
    		}
    	}
    
    	// main method to start the application
    	public static void main(String[] args) {
    		Exer1218659 ttt = new Exer1218659();
    		ttt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    After the line 37 excutes I expect that the ActionHandler (ah) will be removed from all of the buttons but this do not happen, then all the remaining button still responding to the clicks.
    Where is the problem in my code.
    Thank you all

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to remove ActionListener from all of the buttons at the end

    The code defines 2 variables named ah. Remove the local one.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    32
    Rep Power
    0

    Default Re: How to remove ActionListener from all of the buttons at the end

    Thank You Very Much NORM.

Similar Threads

  1. Replies: 1
    Last Post: 02-26-2015, 11:27 PM
  2. ActionListener help
    By nat in forum New To Java
    Replies: 2
    Last Post: 12-14-2013, 09:07 PM
  3. Radio buttons to open list of more radio buttons
    By mcconnelln89 in forum New To Java
    Replies: 1
    Last Post: 11-16-2013, 02:31 AM
  4. Help....add actionListener to buttons
    By jmeats77 in forum New To Java
    Replies: 9
    Last Post: 05-24-2012, 01:11 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
  •