File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes jButtons  are driving me crazy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jButtons  are driving me crazy" Watch "jButtons  are driving me crazy" New topic

jButtons are driving me crazy

Tami Hart

Joined: Jun 09, 2004
Posts: 7
how do I get my buttons to have functionality? the window is set up the way I want it. however, when the user clicks on the calculate button I want it to perform a mathmatical function and then display the results in the payment field.

Please Help

here is my program:

import javax.swing.*;
import java.awt.*;

class PosMortg1 extends JFrame {

PosMortg1() {
super("Calculate a Mortgage Payment?");
setSize(370, 270);
Container pane = getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
JPanel row1 = new JPanel();
JLabel amountLabel = new JLabel("How much are you Borrowing?");
JTextField amount = new JTextField(14);
JPanel row2 = new JPanel();
JLabel percentLabel = new JLabel("Enter Percent Rate:");
JTextField percent = new JTextField(14);
JPanel row3 = new JPanel();
JLabel yearsLabel = new JLabel("How Many Years?");
JTextField years = new JTextField(14);
JPanel row4 = new JPanel();
JButton calculate = new JButton("Calculate");
JPanel row5 = new JPanel();
JLabel paymentLabel = new JLabel("Monthly Payments");
JTextArea payment = new JTextArea(4, 14);
JScrollPane scroll = new JScrollPane(payment,

public static void main(String[] arguments) {
PosMortg1 mortgage = new PosMortg1();
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

I'd say you need to visit Sun's tutorial on Buttons.

GenRocket - Experts at Building Test Data
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
I do not think you need the second last line of your constructor:

But it probably does not do any harm.

The nice thing about Standards is that there are so many to choose from!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Tami asked:

if I want to use the fields the user fills in How do I call them into the same math equation? I need to use all three in the same math equation.

In your event method you can simply call amount.getText() to get a String of the value entered in the textfield. Keep in mind though that you will then need to convert your String to an Integer to do calculations. Also note that the compile will probably complain that your variables like amount, percent, etc will need to be declared final.
[ July 03, 2004: Message edited by: Gregg Bolinger ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Here's how I might do it:

I agree. Here's the link:
subject: jButtons are driving me crazy
It's not a secret anymore!