multiple action listeners?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: multiple action listeners?

  1. #1
    Join Date
    Dec 2005
    Posts
    4

    multiple action listeners?

    hello!

    I have a class here that creates a small menu, the "btn1PlayerButton" opens another class file and the actionListener works fine, but i have two other buttons. One of them is "btnExit" and i want that to close the program, i know the code and have it there.......

    my problem is it assigns both buttons to have the first action, can anyone help me??

    here's my code!
    Code:
    import javax.swing.*;
    import javax.swing.event.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    
    public class Menu1 implements ActionListener 
    { 
    	private static void createAndShowGUI (Menu1 paul) 
    	{  
    		JButton btn1PlayerButton = new JButton ("One Player");
    		JButton btnRules = new JButton ("Rules");
    		JButton btnExit = new JButton ("Exit");
    		
    		Dimension menuButtonSize = new Dimension(110, 25);
    		
    		JLabel lblTitle = new JLabel ("Battleships 3000");
    		JLabel lbl1PlayerGame = new JLabel ("Click to start a one player game");
    		JLabel lblRules = new JLabel ("To see the rules click here");
    		JLabel lblExit = new JLabel ("Exit the program");
    	
    		JFrame frame = new JFrame ("Welcome to BattleShips");
     		Container content = frame.getContentPane();
     		content.setLayout (new BorderLayout());
    	 
     		JPanel north = new JPanel();
    		north.setPreferredSize(new Dimension(110,115));
       
    			north.add(lblTitle);
    			lblTitle.setForeground(Color.blue);
    	
    		content.add(north, BorderLayout.NORTH);
    	
    		JPanel center = new JPanel();
    	
    			JPanel jp = new JPanel();
    			jp.setPreferredSize(new Dimension(160,25)); // Used to create a blank space in menu
    		
    		center.add(jp);
    		content.add(center, BorderLayout.CENTER);
    	
    		JPanel west = new JPanel();
    		west.setLayout(new BorderLayout());
    		JPanel wNorth = new JPanel();
    		JPanel wCenter = new JPanel();
    		JPanel wSouth = new JPanel();
    		wSouth.setPreferredSize(new Dimension(110,115));
    	
    			wNorth.add(lbl1PlayerGame);	
      			wCenter.add(btn1PlayerButton);
    			btn1PlayerButton.setPreferredSize(menuButtonSize);
    			btn1PlayerButton.addActionListener(paul);
       
    		west.add(wNorth, BorderLayout.NORTH);
    		west.add(wCenter, BorderLayout.CENTER);
    		west.add(wSouth, BorderLayout.SOUTH);	
    		content.add(west, BorderLayout.WEST);
    	
    		JPanel east = new JPanel();
    		east.setLayout(new BorderLayout());
    		JPanel eNorth = new JPanel();
    		JPanel eCenter = new JPanel();
    		JPanel eSouth = new JPanel();
    		eSouth.setPreferredSize(new Dimension(110,115));
    	
    			eNorth.add(lblRules);
    			eCenter.add(btnRules);
    			btnRules.setPreferredSize(menuButtonSize);
    	
    		east.add(eNorth, BorderLayout.NORTH);
    		east.add(eCenter, BorderLayout.CENTER);
    		east.add(eSouth, BorderLayout.SOUTH);	
    		content.add(east, BorderLayout.EAST);
    	
    		JPanel south = new JPanel();
    		south.setLayout(new BorderLayout());
    		JPanel sNorth = new JPanel();
    		JPanel sCenter = new JPanel();
    	
    			sNorth.add(lblExit);
    			sCenter.add(btnExit);
    			btnExit.setPreferredSize(menuButtonSize);
    			btnExit.addActionListener(paul);
    			
    		south.add(sNorth, BorderLayout.NORTH);
    		south.add(sCenter, BorderLayout.SOUTH);	
    		content.add(south, BorderLayout.SOUTH);
     
     	 	frame.pack();
     		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	  	frame.setVisible(true);
    	   frame.setSize(550, 400);
    	  	frame.setResizable(false);
    	}
    
    	public void actionPerformed (ActionEvent ae) 
    	{
    		/*Source source = evt.getSource();
    		
    		if (source == btn1PlayerButton)
    		{*/
    			TrialGrid grid = new TrialGrid(); 
    			grid.showGrid();
    		/*}
    		
    		if (source == btnExit)
    		{
    			System.exit(0);
    		}*/
    	} 
    
    	public static void main (String[] args) 
    	{ 
    		javax.swing.SwingUtilities.invokeLater(new Runnable() 
    		{ 
    			public void run() 
    			{ 
    				Menu1 paul = new Menu1(); 
    				createAndShowGUI(paul); 
    			} 
    		});
     	}
    }
    Thank you!
    Last edited by zoidberg; 12-12-2005 at 03:09 PM.

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    Though it isn't always the nicest, I prefer to use the Action object to make JButtons. That way I don't have to do all the e.getSource() stuff. Example:
    Code:
    JButton btn = new JButton(new AbstractAction("Exit") {
      public void actionPerformed(ActionEvent e) {
         System.exit(0); // or whatever you want to put here.
      }
    });
    Hope this helps.
    ~evlich

  3. #3
    Join Date
    Dec 2005
    Posts
    5
    Hi zoidberg,

    evlich tried to use the annonymous inner classes concept. Yes, it will be very handy in these situation, first try to go through the concept of annonymous inner classes, you will find it interesting.

  4. #4
    Join Date
    Dec 2005
    Posts
    19
    it is very obvious that you register your action listeners under one single object, which is "paul". That is why no matter which button you clicked, it will only respond to the first action.

    To avoid this error again, just replace "paul" with the "this" keyword. This should solve the error.

Similar Threads

  1. Replies: 2
    Last Post: 02-13-2002, 10:44 AM
  2. Can't get script to execute an Outlook Action
    By Andrey Fyodorov in forum Enterprise
    Replies: 0
    Last Post: 07-25-2001, 06:37 PM
  3. Replies: 1
    Last Post: 05-31-2000, 09:46 PM
  4. Replies: 1
    Last Post: 05-27-2000, 07:29 AM
  5. Run-Time Error 3061
    By Steve Kocon in forum VB Classic
    Replies: 1
    Last Post: 05-15-2000, 08:42 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center