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

Thread: Problem Regarding ActionListener

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problem Regarding ActionListener

    I was Provided with a topic TicTacToe Game to be Prepared With in One Week by my University . I am a Beginner In this Programming Language

    Actually I am Using An Array of Buttons and and also able to add button action Listener But Cannot able to perform function an a particular button
    This is how I add Various Buttons In Panel having GridLayout


    for(i=0;i<9;i++){
    //Butto[i].setSize(70,70);
    Butto[i]=new Button();
    p1.add(Butto[i]);
    Butto[i].addActionListener(this);

    }


    Now how to add a text on a particular Button When we click on it .
    The actionPerformed Method Which i used is Given below

    public void actionPerformed(ActionEvent e) {

    Butto[i].setLabel(" X ");

    }



    Some Having Good Command on it Please help me How to add 'X' whenever i will click on the button

    Thanx to all, for their Support, If Provided .
    Have a nice Day .
    Last edited by sahilradotra; March 30th, 2012 at 10:03 AM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Problem Regarding ActionListener

    There are two methods in the JButton class (JButton (Java Platform SE 6)) that is important for you here. The first being the addActionListener(ActionListener l) method and the second being the setText(String text) method (both of which are inherited from the AbstractButton class).

    Let's say you have a JButton named button, and you wanted to add an actionListener to the button. You would create the button by saying something like:
    JButton button = new JButton("Button Text");
    In order to add an actionListener to that button, we need to evoke the addActionListener(ActionListener l) method, and send it an ActionListener (ActionListener (Java Platform SE 6)) as the argument. We can do this by either creating a new class that implements the ActionListener interface and then using that as an argument, or we can sort of create it on the go. For the sake of keeping things simple, I'll just show you how to create it on the go. To do it this way, we actually create the ActionListener object inside the argument of the addActionListener(...) method. When creating an ActionListener object, we are required to define what the actionPerformed(ActionEvent e) method in the ActionListener interface will do. Here is an example of code where the phrase: "The Button has been pressed" is printed out to the console whenever the button object is pressed:
    button.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e)
         {
              System.out.println("The Button has been pressed");
         }
    });
    When we press the button, it activates the ActionListener we created, and then evokes the actionPerformed(...) method we defined.

    Now, in order to change the text of a JButton, we use the setText(String text) method. Let's say we wanted to change the text of our button object to: "This is a button". To do that, we would say:
    button.setText("This is a button");

    Now, for your project, you are wanting to evoke the setText(...) method inside the actionPerformed(...) method. I'll let you try to figure out how to combine the two with the information I've provided. If you have any trouble, explain where you are lost and post the segment of code that you're having trouble with so I can help you work through it.
    Last edited by aussiemcgr; March 30th, 2012 at 09:50 AM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    sahilradotra (March 30th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem Regarding ActionListener

    Thanx Sir,

    Actually I forgot to tell about the array of buttons

    now to add text on a particular Button of an array what we should do ...........

    Thanx once again for your help..

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Problem Regarding ActionListener

    Ok, well your problem is this:
    public void actionPerformed(ActionEvent e) {
     
    Butto[i].setLabel(" X ");
     
    }

    First of all, you need to use the setText(...) method, not the setLabel(...) method. Second, the index variable i is not accessible outside of your for loop, so you cannot say: Butto[i] without knowing the index.
    However, the ActionEvent object that is sent to the actionPerformed(...) method may help you. Using the ActionEvent object, we can use the getSource() method, which I think might give us the button. Although it will be returned in the form of an Object, so we need to cast it. Try this code and tell me if it works:
    public void actionPerformed(ActionEvent e) {
    ((JButton)(e.getSource())).setText(" X ");
    }

    If it works, I'll explain in detail to you what I did. If not, we might need to get a bit more creative.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    sahilradotra (March 30th, 2012)

  7. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem Regarding ActionListener

    Thanx Sir It's Working..........
    Thanx a lot for this help.

  8. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Problem Regarding ActionListener

    Do you understand how it works?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem Regarding ActionListener

    Thanx For your help sir
    It's Completed from logic point of view
    Now Designing Part is remaining.
    I think it will be Completed in one or two days.

Similar Threads

  1. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 03:43 PM
  2. Problem with actionlistener/drop-down-menu (for a queue)
    By blueroselara in forum AWT / Java Swing
    Replies: 17
    Last Post: January 25th, 2012, 11:59 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. Simple Actionlistener problem
    By olemagro in forum AWT / Java Swing
    Replies: 5
    Last Post: October 11th, 2010, 10:46 AM
  5. ActionListener Help?
    By Drag01 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 08:21 PM