Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Create or fire automated ActionEvent

  1. #1
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Create or fire automated ActionEvent

    Hey all,

    First off, I like to create my own algorithms, its good practice.

    So I am making a simple Tic Tac Toe game and everything works perfectly except...

    When it is the computers turn I want the computer to figure out which button Jpanel to click or perform an action on (which I have the searching algorithm figured out). I just need to know how to have the computer "automatically" fire the actionevent OR create my own actionevent and fire it.

    Is there a method I should call?

    Any wisdom, hints, tips, or help is appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Create or fire automated ActionEvent

    One way could be to create an ActionEvent object and call the action listener method for the component that you want to send the ActionEvent object to.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Create or fire automated ActionEvent

    thats what I am trying to do. My problem is that I do not know how to successfully create an actionevent object. Here is the calling code:

    	public void begin()
    	{
    		while(true)
    		{
    			if(turn == false)
    			{
    				int toClaim = search();
     
    				ActionEvent e = new ActionEvent(MainGame.grid[1], 1, null);
    				MainGame.grid[toClaim].actionPerformed(e);
    			}
    		}
    	}

    The last two lines of code (the creating object and calling method) are where the trouble is. I just randomly put arguments in for the constructor since I didn't really know what to put there.

    Here is the method being called:

    	public void actionPerformed(ActionEvent e) 
    	{
    		boolean turn = TTEngine.turn;
     
    		if(turn == true)
    			playerTurn();
    		else
    			cpuTurn();
     
    		TTEngine.turn = !turn;
    	}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Create or fire automated ActionEvent

    how to successfully create an actionevent object.
    Do what it says in the API doc when it describes the constructor for the class.

    where the trouble is.
    What trouble?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Create or fire automated ActionEvent

    Trouble = the entire ActionEvent for which this post originates.

    I changed the call to this:

    ActionEvent e = new ActionEvent(this, 1001, null);
    MainGame.grid[toClaim].actionPerformed(e);

    Also, there may now be a logic error, Once fixed Ill let you know if this thread should be solved or not.

Similar Threads

  1. [SOLVED] shooting fire ball 2d
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2012, 05:27 PM
  2. How to write a simple automated reservation JAVA application
    By thekongss in forum Java Theory & Questions
    Replies: 3
    Last Post: November 30th, 2011, 12:31 AM
  3. Unused Variable in ActionEvent()
    By ishtar in forum Java Theory & Questions
    Replies: 5
    Last Post: September 13th, 2011, 05:50 PM
  4. [SOLVED] Problem with ActionEvent not doing anything
    By thisisnic in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2011, 01:13 PM
  5. How to generate an ActionEvent progrematically
    By nasi in forum Java Theory & Questions
    Replies: 7
    Last Post: May 22nd, 2010, 12:31 PM