• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

jTextfields and JButtons

 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello I am working on a Payroll application and I am using the palette to design my GUI. I have four jTextFields which are labeled as Employee Name, Hours worked, and PayRate. Three Jbuttons which are calculate, clear, and exit. I have the exit button and the clear jButtons functioning as they should. My question is how do I calculate (hours worked* payrate) JTextField2 and jTextField3 to be displayed in the jTextField4 box once I have pressed the calculate button? any help is appreciated. The main reason for using the palette is I am not good at coding.

 
Bartender
Posts: 5462
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dalton,

you can get the text from a JTextField with: String s = jtf.getText().

Then, you can get its value by using: value = Double.parseDouble(s), although you should be aware of the possible errors that may be thrown. See the Java API's for this.

Put the calculation code in the actionlistener of the "Calculate" button.

Succes!

Piet
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dalton,
Welcome to the Ranch!

You need to add actionListeners to buttons to perform actions when button is clicked. You have already added such listeners for the 'Exit' button. You need to do similarly for the 'Calculate' button.
You can read more about event handlers here

Also, provide meaningful names to the variables. For example, instead of jTextField1, you can have txtHoursWorked. Likewise, jButton1, you can have btnCalculate. This provides handy information on the component name and the functionality side too.
 
Marshal
Posts: 78660
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Names like textField1 are usualy supplied automatically by the IDE. It is probably a good idea to stop using NetBeans. For one thing you won’t have all that illegible group layout code with the long lines.
 
Piet Souris
Bartender
Posts: 5462
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Opinions on whether or not to use programs like NetBeans or Eclipse tend to be very mixed. In my view, the advantages they offer are invaluable: syntax checking, doing the correct imports, and running and jarring the program at the click of a button, to name just a few. True, it takes some time to be able to use these programs (they never stop to look daunting), but for me, the advantages far outweigh the disadvantages. I certainly would advise any beginner to use them.
 
Campbell Ritchie
Marshal
Posts: 78660
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote: . . . NetBeans or Eclipse . . . I certainly would advise any beginner to use them.

I would certainly warn any beginner against going anywhere near them. They have a steep learning curve, which will cause information overload when you need to learn programming. They hide the basic operations from the user, preventing one from understanding the instructions sent to the OS (command line).

Nobody who has not got enough experience to know what jarring is (I presume you mean creating .jar files; I have never seen that word used like that before) should go anywhere near an IDE.
 
Do not threaten THIS beaver! Not even with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic