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 .
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:
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:
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:
Code java:
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.
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..
Re: Problem Regarding ActionListener
Ok, well your problem is this:
Code java:
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:
If it works, I'll explain in detail to you what I did. If not, we might need to get a bit more creative.
Re: Problem Regarding ActionListener
Thanx Sir It's Working..........
Thanx a lot for this help.
Re: Problem Regarding ActionListener
Do you understand how it works?
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.