Results 1 to 7 of 7
  1. #1
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default JButton ActionListener problems

    I'm trying to make a JPanel with two JButtons, but I'm having some problems setting up the actionlisteners for the buttons. Whenever I compile my code, I get 20 errors, mostly that the class, interface, or enum was expected. The first error I get is <identifier> expected when I try to use the JButton's addActionListener method on line 11. Here's my code:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    public class AutoMouse extends JPanel implements ActionListener
    {
    
      public boolean autoTest = true;
      public JButton button1 = new JButton("Begin");
      button1.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          autoTest = true;
          try
          {
            Robot robot = new Robot();
            Random random = new Random();
    
            while(autoTest)
            {
              robot.mousePress(InputEvent.BUTTON1_MASK);
              robot.mouseRelease(InputEvent.BUTTON1_MASK);
              robot.delay(500 + (random.nextInt(750)+ 5));
              robot.mousePress(InputEvent.BUTTON1_MASK);
              robot.mouseRelease(InputEvent.BUTTON1_MASK);
              robot.delay(3500 + (random.nextInt(1500) + 5));
            }
          }
          catch (Exception e)
          {
            e.printStackTrace();
          }
        }
      });
      public JButton button2 = new JButton("Stop");
      {
      button2.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          autoTest = false;
        }
      });
    
      public AutoMouse()
      {
        super();
        add(button1);
        add(button2);
        setBackground(Color.WHITE);
        setSize(100,100);
        setFocusable(true);
        setDoubleBuffered(true);
      }
    }
    Thanks for looking at this if you do.

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

    Default

    You're trying to make method calls in the class here:

    Java Code:
      public boolean autoTest = true;
      public JButton button1 = new JButton("Begin");
      button1.addActionListener(new ActionListener()  // **** here **** 
      {
    outside of any method or constructor and pure the rules of basic Java (forgetting Swing), this simply can't be be done. Do this and similar method calls in your constructor. Again, this is nothing more than basic Java 101. An equivalent simpler problem would be:


    Java Code:
    // ***** incorrect
    public class Foo4 {
        System.out.println("Hello");
    }


    Java Code:
    // ***** correct as calling code in method
    public class Foo4 {
      public void myMethod() {
        System.out.println("Hello");
      }
    }

    Java Code:
    // ***** also correct as calling code in constructor
    public class Foo4 {
      public Foo4() {
        System.out.println("Hello");
      }
    }
    Last edited by Fubarable; 02-05-2011 at 04:03 PM.

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

    Default

    More importantly, that you have a lot of code here with this error shows shows us that your style of coding is potentially harmful, that you're adding a lot of coding and compiling infrequently. You should compile your code frequently, often after adding every line, and you should not add any code to a class until all compile errors are fixed.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    On a first look I noticed this: your ActionEvent parameter is named e and your Exception is also named e; the compiler doesn't like that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    ...and if your write implements ActionListener , your class have to implement the actionPerfromed method !
    And public void actionPerformed(ActionEvent e) and catch (Exception e) { , you have to rename one of them (duplicate) !

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

    Default

    In sum: this code is completely and utterly borked. Scrap it completely and start over, but again compiling after every line and fixing every error before adding any more code.

  7. #7
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Whoa, well, I guess this is what I get when I take time off of coding and try to jump right back into it without really thinking about what I'm doing. Thanks for helping me with this, and I'll go and try this again with a fresh notepad. Thanks for the help, Fubarable, JosAH, and eRaaaa!

Similar Threads

  1. JButton setColor problems
    By brachaalizah in forum AWT / Swing
    Replies: 1
    Last Post: 10-29-2010, 04:10 AM
  2. JButton actionlistener not working, very strange
    By alacn in forum New To Java
    Replies: 4
    Last Post: 08-05-2010, 02:25 AM
  3. Problems with ActionEvent for JButton
    By TrueBear in forum AWT / Swing
    Replies: 1
    Last Post: 08-24-2009, 04:26 PM
  4. Replies: 2
    Last Post: 04-19-2009, 05:39 AM
  5. Problems with jButton ActionPerformed
    By susan in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2007, 04:19 AM

Tags for this Thread

Posting Permissions

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