File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using MouseListeners to create a calculator

 
Isuru Buddhika
Greenhorn
Posts: 11
Firefox Browser Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im developing a simple calculator and I want to set the text of the TextField to label of the button clicked. Can anyone tell me how to do this?
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isuru Buddhika wrote:... set the text of the TextField ...

Swing, AWT or JavaFX2?
 
Michael Dunn
Ranch Hand
Posts: 4632
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Michael. Isuru, please BeForthrightWhenCrossPostingToOtherSites <-click the link.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoever it was on SO who told you to use ActionListeners and not MouseListeners was correct.
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always provided this is about Swing or AWT.
 
Isuru Buddhika
Greenhorn
Posts: 11
Firefox Browser Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody who helped me in various ways.. And can some one tell me why it is good to use ActionListeners rather than using MouseListeners?
 
Paul Clapham
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because ActionListener is specifically designed to be called when a button is clicked. You could use a MouseListener but then you have to do extra work if you want to know basic things like which button was clicked.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic