This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes counter is not shown in JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "counter is not shown in JTextField" Watch "counter is not shown in JTextField" New topic
Author

counter is not shown in JTextField

Anissa Pary
Ranch Hand

Joined: Jan 02, 2012
Posts: 40
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!

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 02, 2012
Posts: 40
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=(
Rod Singh
Greenhorn

Joined: Feb 10, 2012
Posts: 16


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

Joined: Jan 02, 2012
Posts: 40
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

Joined: Feb 10, 2012
Posts: 16

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

Joined: Jan 02, 2012
Posts: 40
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

Joined: Feb 10, 2012
Posts: 16
You're welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: counter is not shown in JTextField
 
Similar Threads
Paint program
Please need help with calculator program...
How to add MouseLsitener to JMenu
How to add a JLabel in any leaf of a JTree
JDialog won't display the another class with Components in JPanel