• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

counter is not shown in JTextField

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello I wanted to make 2 buttons 1 is increment which increases counter by 1, the other is reset to make counter to be zero and start counting from the beginning. Moreover, I wanted to display the counter value in text field.However, I dunno why my code does not show counter's value. Please can anyone help!

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why on earth are you using a MouseListener to cause an action to happen when a JButton is clicked? The standard way to do that is to add an ActionListener to the JButton; using a mouse listener is just confusing.

But let's suppose that such a MouseListener actually does what you think it will do. Your implementation of MouseListener doesn't do anything but set a boolean variable, as far as I can see. It doesn't increment anything, nor does it reset anything, nor does it change the value of any text fields.
 
Anissa Pary
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Why on earth are you using a MouseListener to cause an action to happen when a JButton is clicked? The standard way to do that is to add an ActionListener to the JButton; using a mouse listener is just confusing.

But let's suppose that such a MouseListener actually does what you think it will do. Your implementation of MouseListener doesn't do anything but set a boolean variable, as far as I can see. It doesn't increment anything, nor does it reset anything, nor does it change the value of any text fields.


Thanks I have changed it 100 times and have not noticed that I deleted that line but still it does not even show " It is " which is in while(true) loop=(
 
Greenhorn
Posts: 16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What I've just tried to help you out that -- how your Q1 would get access to the TextField item --so that the increment can be displayed. Notice that the MyFrame object is passed to the IncrementResetListener() method.
 
Anissa Pary
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rod Singh, Thank you so much! One more stupid question I don't know how but you made the counter increase each time when I press the button. How did you do that?? I just a little bit confused=( I just tried before and it stopped running after first time. So, when I press the button for the second time it did not increase. How did you manage???=/
 
Rod Singh
Greenhorn
Posts: 16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Replace the mouseClicked() method, which is in the previous posting with the one given below.



The "increment", and the "reset" is handled in the mouseClicked() method. As per the code-- from my previous posting-- this event handler method is attached with both the buttons.

That being stated, when the button labeled as "increment" is clicked-- the count is creased by 1, and the text field is set with the new value of the count. On the other hand, when the button labeled as "reset" is clicked--the count is reset to 1, and the text field is reset to zero length string--a blank.

I hope you understood it.


 
Anissa Pary
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rod Singh wrote:
Replace the mouseClicked() method, which is in the previous posting with the one given below.



The "increment", and the "reset" is handled in the mouseClicked() method. As per the code-- from my previous posting-- this event handler method is attached with both the buttons.

That being stated, when the button labeled as "increment" is clicked-- the count is creased by 1, and the text field is set with the new value of the count. On the other hand, when the button labeled as "reset" is clicked--the count is reset to 1, and the text field is reset to zero length string--a blank.

I hope you understood it.



yeah, fair enough=) Thank you by the way!=)
 
Rod Singh
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
reply
    Bookmark Topic Watch Topic
  • New Topic