aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes jTextfields and JButtons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jTextfields and JButtons" Watch "jTextfields and JButtons" New topic
Author

jTextfields and JButtons

Dalton Paleo
Greenhorn

Joined: May 04, 2013
Posts: 1
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.

Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 687
    
  11
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
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

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.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
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
Ranch Hand

Joined: Mar 08, 2009
Posts: 687
    
  11
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
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
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.
 
 
subject: jTextfields and JButtons