Results 1 to 2 of 2
  1. #1
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default ActionListener run automatically problem

    Hi, something weird happen to my program.

    Before I click the button, it seems that the program run my action listener automatically. The ....This shouldnt happen to appear on the console before button clicked...appear in the console before the button clicked.

    What is wrong with the code?

    and another weird thing happen the last else also shown in the console...but I only have 2 buttons.

    Java Code:
         ......	
         buttonX.addActionListener(this);     
         buttonY.addActionListener(this);
    
    
    public void actionPerformed(ActionEvent e) {	
         .....
         System.out.println ([COLOR="Blue"]"This shouldnt happen to appear on the console before button clicked[/COLOR]");
         if(e.getSource ()==buttonX)
              System.out.println ("Answer 1")
         else if (e.getSource()==buttonY)
              System.out.println ("Answer 2")
         else
         [COLOR="Blue"]      System.out.println ("This should not happen");[/COLOR]
         ...... 
       }
    Last edited by cassysumandak; 03-23-2009 at 10:17 PM.

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

    Default

    I don't see anything wrong with the code that you're showing us, but likely you have a bug somewhere else in code that is not being shown.

Similar Threads

  1. Application automatically terminates
    By PaulT in forum Eclipse
    Replies: 4
    Last Post: 03-18-2009, 05:02 PM
  2. ActionListener Applet problem
    By xander5511 in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 03:42 AM
  3. Java actionlistener help
    By justsomeguy in forum AWT / Swing
    Replies: 1
    Last Post: 05-27-2008, 05:42 AM
  4. Demonstrating the ActionListener
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 04-23-2008, 08:20 PM
  5. Call a Method Automatically
    By rhm54 in forum New To Java
    Replies: 4
    Last Post: 02-07-2008, 08:51 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
  •