aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JButton not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton not working" Watch "JButton not working" New topic
Author

JButton not working

Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
I am trying to get my program to do a math calculation when you click on the JButton, but nothing happens. Any suggestions and pointers would be appreciated.

J

import javax.swing.*; //importing the ability to create and use a frame
import java.awt.*; //importing the swing functions like the combo box
import java.awt.event.*;
import java.lang.String;

public class MortgageFrame4 extends JFrame implements ItemListener, ActionListener, Runnable{

String paythis;
double monthly;
int a;
int t;
double I;
double total;
int loop;
double balance;
String actInt;
JLabel amountLabel = new JLabel ("House cost in dollars:"); //adding a label for one of my boxes
JComboBox amount = new JComboBox();{ //going to use the drop down box
amount.addItem("$100,000"); //adding the optional amounts
amount.addItem("$125,000");
amount.addItem("$150,000");
amount.addItem("$175,000");
amount.addItem("$200,000");
amount.addItem("$225,000");
amount.addItem("$250,000");
amount.addItem("$275,000");
}
JLabel tILabel = new JLabel ("Payment term and rate:");
String[] termsInterestArray = {"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};
String[] termsArray = {"7 years", "15 years", "30 years"};
String[] interestArray = {"5.35%", "5.5%", "5.75%"};
JComboBox tI = new JComboBox(termsInterestArray);
JComboBox term = new JComboBox(termsArray);
JComboBox interest = new JComboBox(interestArray);
JButton calculate = new JButton("Calculate");
JButton quit = new JButton("Quit");

JLabel heading = new JLabel (" Payment # | Loan Balance | Interest Paid");
JTextArea future = new JTextArea (45, 365);
JScrollPane futureInfo = new JScrollPane(future);

JCheckBox seperateCheck = new JCheckBox("Check to select term and interest seperately");
JCheckBox tICheck = new JCheckBox("Check to select term and interest together");
ButtonGroup select = new ButtonGroup();

public MortgageFrame4(){
setTitle("Mortgage calculator"); //giving my frame a title
setSize(340,350); //setting the size of the frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //how the frame will close
setVisible(true); //user can see the box
FlowLayout flo = new FlowLayout();
Container pane = getContentPane(); //time to add stuff inside the frame
future.setLineWrap(true);

select.add(seperateCheck);
select.add(tICheck);

pane.setLayout(flo); //setting the default layout manager
pane.add(amountLabel);
pane.add(amount); //adding the amount button to the container
pane.add(tILabel);
pane.add(tICheck);
pane.add(tI); //adding the term line to the container
pane.add(seperateCheck);
pane.add(term); //adding the term line to the container
pane.add(interest); //adding the term line to the container
pane.add(calculate);
pane.add(quit);

pane.add(heading);
pane.add(futureInfo);
setContentPane(pane);

amount.addItemListener(this);//setting up the listeners
tI.addItemListener(this);
term.addItemListener(this);
interest.addItemListener(this);
seperateCheck.addItemListener(this);
tICheck.addItemListener(this);
calculate.addActionListener(this);
quit.addActionListener(this);
repaint();
}
//this is where the program will act upon the user selections
public void itemStateChanged(java.awt.event.ItemEvent e){

}
//This is where the calculate button functionality comes from
public void actionPerformed(ActionEvent event){
if(event.getSource() == calculate){
if (amount.equals("$100,000")){
a = 100000;
}
else if (amount.equals("$125,000")){
a = 125000;
}
else if (amount.equals("$150,000")){
a = 150000;
}
else if (amount.equals("$175,000")){
a = 175000;
}
else if (amount.equals("$200,000")){
a = 200000;
}
else if (amount.equals("$225,000")){
a = 225000;
}
else if (amount.equals("$250,000")){
a = 250000;
}
else if (amount.equals("$275,000")){
a = 275000;
}
if (seperateCheck.isSelected()){
if (term.equals("7 years")){
t = 7*12;
}
else if (term.equals("15 years")){
t = 15*12;
}
else if (term.equals("30 years")){
t = 30*12;
}
if (interest.equals("5.35%")){
I = 0.0535;
}
else if (interest.equals("5.5%")){
I = 0.055;
}
else if (interest.equals("5.75%")){
I = 0.0575;
}
}
else if (tICheck.isSelected()){
if (tI.equals("7 years at 5.35%")){
t = 7*12;
I = 0.0535;
}
else if (tI.equals("15 years at 5.5%")){
t = 15*12;
I = 0.055;
}
else if (tI.equals("30 years at 5.75%")){
t = 30*12;
I = 0.0575;
}
}
total = (a*t*I);
balance = (total - (monthly*loop));
actInt = ("$" + (monthly * I));
}
else if(event.getSource() == quit){
System.exit(0);
}
}
public void run(){
while (balance >= 0){
for (int loop = 1; loop <= t; loop ++){
future.setText(loop + balance + actInt);
repaint();
}
}
}
public static void main(String[] arguments){ //this is the main function where the program runs
MortgageFrame4 mor = new MortgageFrame4();
}
}
Sebastiaan Kortleven
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
you'r checking to see if amount equals $100.000 (eg), but amount is a jcombobox, so you should check to see if amount.getSelectedItem() equals $100.000 (same for the other comboboxes)
Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
Thanks

I tried a couple different versions of that, but clearly have the syntax wrong because all I got was a bunch of different errors.

I tried to find "getSelectedItem" in my API so I could look at the methods that it can be used in, but was unable to find it.

I tried:
if (amount.getSelectedItem(equals("$100,000")));
and
if (amount.getSelectedItem equals("$100,000"));

Neither of which was right Can I use the "=" symbol instead or should I try a different approach? I will continue to look in the API, maybe I spelled it wrong.

Thanks,
Jen
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
The method getSelectedItem() returns type Object, this will need to be cast to a String. the the following is correct

Also think of how to avoid using if else so many times, would a switch statement improve the readability of your code and make it easier to maintain?
[ November 22, 2004: Message edited by: Nigel Browne ]
Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
Nigel,

When I write the line that way I get:

MortgageFrame4.java.99: inconvertible types
found: boolean
required jave.lang.String

So I tried this instead:

if (String.valueOf(amount.getSelectedItem().equals("$100,000"))){
a = 100000;
}
and got an incompatible types error instead.

Jen
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Try
Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
Nigel,
I made your suggested changes and the code compiles now. Thanks

However when I press the calculate button I still do not get anything to print to the text area. The entire code now looks like this:

import javax.swing.*; //importing the ability to create and use a frame
import java.awt.*; //importing the swing functions like the combo box
import java.awt.event.*;
import java.lang.String;

public class MortgageFrame4 extends JFrame implements ItemListener, ActionListener, Runnable{

//setting up my variables
String paythis;
double monthly;
int a;
int t;
double I;
double total;
int loop;
double balance;
String actInt;

//creating lables, boxes, arrays, and buttons
JLabel amountLabel = new JLabel ("House cost in dollars:");
JComboBox amount = new JComboBox();{
amount.addItem("$100,000");
amount.addItem("$125,000");
amount.addItem("$150,000");
amount.addItem("$175,000");
amount.addItem("$200,000");
amount.addItem("$225,000");
amount.addItem("$250,000");
amount.addItem("$275,000");
}
JLabel tILabel = new JLabel ("Payment term and rate:");
String[] termsInterestArray = {"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};
String[] termsArray = {"7 years", "15 years", "30 years"};
String[] interestArray = {"5.35%", "5.5%", "5.75%"};
JComboBox tI = new JComboBox(termsInterestArray);
JComboBox term = new JComboBox(termsArray);
JComboBox interest = new JComboBox(interestArray);
JButton calculate = new JButton("Calculate");
JButton quit = new JButton("Quit");
JLabel heading = new JLabel (" Payment # | Loan Balance | Interest Paid");
JTextArea future = new JTextArea (45, 365);
JScrollPane futureInfo = new JScrollPane(future);

JCheckBox seperateCheck = new JCheckBox("Check to select term and interest seperately");
JCheckBox tICheck = new JCheckBox("Check to select term and interest together");
ButtonGroup select = new ButtonGroup();

public MortgageFrame4(){
//setting up the frame and frame characteristics
setTitle("Mortgage calculator");
setSize(340,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); //user can see the box
FlowLayout flo = new FlowLayout();
Container pane = getContentPane();
future.setLineWrap(true);
select.add(seperateCheck);
select.add(tICheck);

//setting the default layout manager and adding everything to the container
pane.setLayout(flo);
pane.add(amountLabel);
pane.add(amount);
pane.add(tILabel);
pane.add(tICheck);
pane.add(tI);
pane.add(seperateCheck);
pane.add(term);
pane.add(interest);
pane.add(calculate);
pane.add(quit);
pane.add(heading);
pane.add(futureInfo);
setContentPane(pane);

//setting up the listeners
amount.addItemListener(this);
tI.addItemListener(this);
term.addItemListener(this);
interest.addItemListener(this);
seperateCheck.addItemListener(this);
tICheck.addItemListener(this);
calculate.addActionListener(this);
quit.addActionListener(this);
repaint();
}
//this is where the program will act upon the user selections
public void itemStateChanged(java.awt.event.ItemEvent e){

}
//This is where the calculate button functionality comes from
public void actionPerformed(ActionEvent event){
if(event.getSource() == calculate){
String strA = (String)amount.getSelectedItem();
if (strA.equals("$100,000")){
a = 100000;
}
else if (strA.equals("$125,000")){
a = 125000;
}
else if (strA.equals("$150,000")){
a = 150000;
}
else if (strA.equals("$175,000")){
a = 175000;
}
else if (strA.equals("$200,000")){
a = 200000;
}
else if (strA.equals("$225,000")){
a = 225000;
}
else if (strA.equals("$250,000")){
a = 250000;
}
else if (strA.equals("$275,000")){
a = 275000;
}
if (seperateCheck.isSelected()){
String strT = (String)term.getSelectedItem();
if (strT.equals("7 years")){
t = 7*12;
}
else if (strT.equals("15 years")){
t = 15*12;
}
else if (strT.equals("30 years")){
t = 30*12;
}
String strI = (String)interest.getSelectedItem();
if (strI.equals("5.35%")){
I = 0.0535;
}
else if (strI.equals("5.5%")){
I = 0.055;
}
else if (strI.equals("5.75%")){
I = 0.0575;
}
}
else if (tICheck.isSelected()){
String strT = (String)tI.getSelectedItem();
if (strT.equals("7 years at 5.35%")){
t = 7*12;
I = 0.0535;
}
else if (strT.equals("15 years at 5.5%")){
t = 15*12;
I = 0.055;
}
else if (strT.equals("30 years at 5.75%")){
t = 30*12;
I = 0.0575;
}
}
total = (a*t*I);
balance = (total - (monthly*loop));
actInt = ("$" + (monthly * I));
}
else if(event.getSource() == quit){
System.exit(0);
}
}
public void run(){
while (balance >= 0){
for (int loop = 1; loop <= t; loop ++){
future.setText(loop + balance + actInt);
repaint();
}
}
}
//this is the main function where the program runs
public static void main(String[] arguments){
MortgageFrame4 mor = new MortgageFrame4();
}
}

Jen
Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
My button works now, thanks so much all for your help

Strangely enough, the loops all print the exact same line, and I can't for the life of me get them to print on different lines, my math formula that I just added is way off, but one thing at a time

Thanks again,
Jen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JButton not working
 
Similar Threads
Interesting exception errors
cannot resolve symbol
combo box
last week of class last assignment. Any help is appreciated.
illegal start of type error