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 10 of 10

Thread: Help with button actionlistener

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Devilish
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Help with button actionlistener

    hey guys i need a little help in java...i am trying to make small game of tic tac toe....can anyone telll me how can i store the text or number displayed on top of a button in a array,when that button is pressed.which methods or function would i have to use in the actionlistener?
    plz help me thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with button actionlistener

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    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: Help with button actionlistener

    how can i store the text or number displayed on top of a button in a array
    What do you need to store in the array?
    The JComponent class has ClientProperty methods you can use to store information in each button itself.

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Devilish
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with button actionlistener

    all the buttons are layed out in a grid manner.the grid is of 3*3 and there are 9 buttons in total.each button has a number on top of it, for example the top leftmost button has the number 1 on it,followed by number 2 and so on.i want to store that number of the button pressed by the user in an array.

  5. #5
    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: Help with button actionlistener

    .i want to store that number of the button pressed by the user in an array.
    You can get the label from the button that was pressed. Look at the API doc for the button class for what method to use.
    Then you can store the number in the array.

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Devilish
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with button actionlistener

    should i use simple action listener or mouse action listener. since the button will be clicked by the mouse.

  7. #7
    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: Help with button actionlistener

    Try the action listener
    Some buttons can get the focus which means you can use the tab and press Enter to get an action event.

  8. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Devilish
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with button actionlistener

    i am trying to use this piece of code....but i am not sure if its correct of nor:

    private class HandlerClass implements ActionListener {
    public void actionPerformed(ActionEvent event){


    buttonpressed[1+event.getID()]=1;
    System.out.println(buttonpressed[1]);


    }

    }

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with button actionlistener

    Quote Originally Posted by umerahmad View Post
    i am trying to use this piece of code....but i am not sure if its correct of nor:
    I promise that if you can't tell whether something is correct, neither can we. What does it do? Does it do what you expect?

    If you want more help, you'll have to provide an SSCCE that demonstrates what you're talking about.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    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: Help with button actionlistener

    buttonpressed[1+event.getID()]=1;
    This looks like buttonpressed is an array of int.
    Do you know know what the value returned by getID is?
    Read the API doc for the event class to see if it is useful for your program.

Similar Threads

  1. Help with ActionListener please
    By knightmetal in forum AWT / Java Swing
    Replies: 3
    Last Post: August 23rd, 2011, 05:41 PM
  2. ActionListener help
    By QBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 12:25 PM
  3. [SOLVED] ActionListener help
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 14th, 2010, 06:57 PM
  4. ActionListener Help?
    By Drag01 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 08:21 PM
  5. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 08:21 PM