Re: Incrementing button by 1
Quote:
Originally Posted by
joshft91
I'm somewhat new to GUI's, and the teacher is of no help at all.
Sorry, but that's no excuse. Many people here are self taught, so you have a leg up on them. Don't waste time complaining about your instructor.
Hint- Where are you incrementing anything? Where are you setting the text of the button? What are you doing when the button is pressed?
Re: Incrementing button by 1
I wasn't wasting my time... All I was saying was that I don't have a lot of other places to go for help.
I know I'm going to be incrementing it in the actionPerformed method. The text is set here
Code :
button = new JButton(new Integer(0).toString());
and as of right now it's not doing anything when the button is pressed.
Re: Incrementing button by 1
Although all of Kevin's advice leads directly to the answer, be aware that getting the source of the event for one simple button is not required if you only attach the listener to only that button.
So whatever you have in actionPerformed() will be done when you click on your JButton.
When or if you do start attaching a listener to multiple buttons, the efficient way to go about it would be to use:
Object src = event.getSource();
then check against src each time with say, if statements.
Just some added detail.
Re: Incrementing button by 1
Quote:
Originally Posted by
joshft91
I wasn't wasting my time... All I was saying was that I don't have a lot of other places to go for help.
That's fine. But taking the time to complain about your instructor is indeed wasteful- if not to you, then to us. Sorry, but I get annoyed with people saying "my teacher won't even help me" when some of the best programmers learned without the benefit of an instructor/class at all.
Quote:
Originally Posted by
joshft91
I know I'm going to be incrementing it in the actionPerformed method. The text is set here
Code :
button = new JButton(new Integer(0).toString());
and as of right now it's not doing anything when the button is pressed.
Why don't you do anything when the button is pressed?
Re: Incrementing button by 1
Edit: Got it working -
Code Java:
public void actionPerformed(ActionEvent event) {
buttonCount++;
button.setText(Integer.toString(buttonCount));
}
The second part to this problem is to have two buttons (updated original post) and to increment when either one is clicked.
Obviously the way it's set up now both will increment... How would I go about making sure only the one I click increments?
Re: Incrementing button by 1
Either add a different ActionListener to each button, or check the source of the event using event.getSource().
Re: Incrementing button by 1
Alright... Well I tried something like this - it compiles fine, but it increments the 2nd button only. :S
Code Java:
public void actionPerformed(ActionEvent event) {
if (event.equals(button1)){
button1Count++;
button1.setText(Integer.toString(button1Count));
}
else {
button2Count++;
button2.setText(Integer.toString(button2Count));
}
}
Is it possible to do what I'm trying to do with an if statement?
Re: Incrementing button by 1
It is possible. But why would the event equal one of the JButtons? Check out the API for ActionEvent, especially the getSource() function. You were actually already told how to do this, and in fact, your original code contained the line you want.
Re: Incrementing button by 1
It appears that this works...
Code Java:
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(button1)) {
button1Count++;
button1.setText(Integer.toString(button1Count));
}
else {
button2Count++;
button2.setText(Integer.toString(button2Count));
}
}
Re: Incrementing button by 1
Cool. Makes sense.
Another way to do this would be to create your own ActionListener Object that keeps track of how many times its actionPerformed() method was called. Then create two instances of that Object- one for each button. That way you wouldn't have to specifically keep track of each button in a separate count.
If that doesn't make sense, don't worry about it. Your way is fine too.