1,105,581 Community Members

count button clicks

Member Avatar
sofien.fkih
Light Poster
31 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I want to count the number of times the button is clicked using GUI.

I did this code:

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
  {                                         
    int clicked = 0;
    clicked++;
    System.out.println(clicked);
  }    

But it showing the output "1", each time I click the button.

I want every time I click the button to show me the count.

ex: If I click the button two times it should give me output of "2".

Member Avatar
JamesCherrill
... trying to help
10,411 posts since Apr 2008
Reputation Points: 2,094 [?]
Q&As Helped to Solve: 1,753 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

You declare int clicked inside the action performed method, so you get a new clicked variable (initialised to 0) every time the method is called.
If you move the declaration to just outside the method then it will be an instance variable that keeps its value between calls to jButton1ActionPerformed

Member Avatar
IIM
Master Poster
742 posts since Jun 2011
Reputation Points: 163 [?]
Q&As Helped to Solve: 169 [?]
Skill Endorsements: 24 [?]
 
-1
 

declaring clicked global will solve ur problem

Member Avatar
JamesCherrill
... trying to help
10,411 posts since Apr 2008
Reputation Points: 2,094 [?]
Q&As Helped to Solve: 1,753 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

No it won't. This is Java. Declaring clicked global will simply create a compiler error. Please don't confuse Java beginners by using non-Java terminology and giving advice that they can't follow, and wouldn't work even if they did.

ps: DaniWeb Member Rules include: "Do post in full-sentence English". Members with English as their second (or third...) language won't uinderstand "ur".

Member Avatar
IIM
Master Poster
742 posts since Jun 2011
Reputation Points: 163 [?]
Q&As Helped to Solve: 169 [?]
Skill Endorsements: 24 [?]
 
0
 

@jAMES - I MEAN DECLARING CHECKED AS INSTANCE VARIABLE,i.e.,Defined at Instace/Object Level
If CHECKED is common to all objects,then use static modifier.

Member Avatar
JamesCherrill
... trying to help
10,411 posts since Apr 2008
Reputation Points: 2,094 [?]
Q&As Helped to Solve: 1,753 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Yes, I understood that was what you meant. (The same solution as detailed in my first post!). I commented because you cannot assume that a beginner will understand your use of the non-Java word "global", that's all.
J

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: