There are no errors or anything in my code, and the program runs and works. However, when I only enter a value in one of the text fields to test the error message, it enters "0" in all of the text fields below it. How can I get it so that it leaves them blank?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class PaintCalculator extends JFrame
{
// declarations
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
JLabel roomWidthJLabel;
JTextField roomWidthJTextField;
JLabel roomLengthJLabel;
JTextField roomLengthJTextField;
JLabel gallonQuantityJLabel;
JTextField gallonQuantityJTextField;
JLabel subTotalJLabel;
JTextField subTotalJTextField;
JLabel salesTaxJLabel;
JTextField salesTaxJTextField;
JLabel totalSalesJLabel;
JTextField totalSalesJTextField;
JButton enterJButton;
JButton clearJButton;
JButton closeJButton;
DecimalFormat currency;
double roomWidth;
double roomLength;
double gallonQuantity;
double subTotal;
double salesTax;
double totalSales;
double TAX_RATE;
double roomArea;
public PaintCalculator()
{
createUserInterface();
}
public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(Color.white);
contentPane.setLayout(null);
roomWidthJLabel = new JLabel();
roomWidthJLabel.setBounds(50, 30, 200, 20);
roomWidthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
roomWidthJLabel.setText("Enter width of room (Feet):");
roomWidthJLabel.setForeground(black);
roomWidthJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(roomWidthJLabel);
roomWidthJTextField = new JTextField();
roomWidthJTextField.setBounds(225, 30, 75, 20);
roomWidthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
roomWidthJTextField.setHorizontalAlignment(JTextField.CENTER);
roomWidthJTextField.setForeground(black);
roomWidthJTextField.setBackground(white);
roomWidthJTextField.setEditable(true);
contentPane.add(roomWidthJTextField);
roomLengthJLabel = new JLabel();
roomLengthJLabel.setBounds(50, 60, 200, 20);
roomLengthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
roomLengthJLabel.setText("Enter length of room (Feet):");
roomLengthJLabel.setForeground(black);
roomLengthJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(roomLengthJLabel);
roomLengthJTextField = new JTextField();
roomLengthJTextField.setBounds(225, 60, 75, 20);
roomLengthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
roomLengthJTextField.setHorizontalAlignment(JTextField.CENTER);
roomLengthJTextField.setForeground(black);
roomLengthJTextField.setBackground(white);
roomLengthJTextField.setEditable(true);
contentPane.add(roomLengthJTextField);
gallonQuantityJLabel = new JLabel();
gallonQuantityJLabel.setBounds(50, 110, 200, 20);
gallonQuantityJLabel.setFont(new Font("Default", Font.PLAIN, 12));
gallonQuantityJLabel.setText("Number of Gallons");
gallonQuantityJLabel.setForeground(black);
gallonQuantityJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(gallonQuantityJLabel);
gallonQuantityJTextField = new JTextField();
gallonQuantityJTextField.setBounds(225, 110, 75, 20);
gallonQuantityJTextField.setFont(new Font("Default", Font.PLAIN, 12));
gallonQuantityJTextField.setHorizontalAlignment(JTextField.CENTER);
gallonQuantityJTextField.setForeground(black);
gallonQuantityJTextField.setBackground(white);
gallonQuantityJTextField.setEditable(false);
contentPane.add(gallonQuantityJTextField);
subTotalJLabel = new JLabel();
subTotalJLabel.setBounds(50, 140, 200, 20);
subTotalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJLabel.setText("Sub Total:");
subTotalJLabel.setForeground(black);
subTotalJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(subTotalJLabel);
subTotalJTextField = new JTextField();
subTotalJTextField.setBounds(225, 140, 75, 20);
subTotalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJTextField.setHorizontalAlignment(JTextField.CENTER);
subTotalJTextField.setForeground(black);
subTotalJTextField.setBackground(white);
subTotalJTextField.setEditable(false);
contentPane.add(subTotalJTextField);
salesTaxJLabel = new JLabel();
salesTaxJLabel.setBounds(50, 170, 200, 20);
salesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJLabel.setText("Sales Tax (7%):");
salesTaxJLabel.setForeground(black);
salesTaxJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(salesTaxJLabel);
salesTaxJTextField = new JTextField();
salesTaxJTextField.setBounds(225, 170, 75, 20);
salesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
salesTaxJTextField.setForeground(black);
salesTaxJTextField.setBackground(white);
salesTaxJTextField.setEditable(false);
contentPane.add(salesTaxJTextField);
totalSalesJLabel = new JLabel();
totalSalesJLabel.setBounds(50, 200, 200, 20);
totalSalesJLabel.setFont(new Font("Default", Font.PLAIN, 12));
totalSalesJLabel.setText("Total Sales:");
totalSalesJLabel.setForeground(black);
totalSalesJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(totalSalesJLabel);
totalSalesJTextField = new JTextField();
totalSalesJTextField.setBounds(225, 200, 75, 20);
totalSalesJTextField.setFont(new Font("Default", Font.PLAIN, 12));
totalSalesJTextField.setHorizontalAlignment(JTextField.CENTER);
totalSalesJTextField.setForeground(black);
totalSalesJTextField.setBackground(white);
totalSalesJTextField.setEditable(false);
contentPane.add(totalSalesJTextField);
enterJButton = new JButton();
enterJButton.setBounds(25, 300, 100, 20);
enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
clearJButton = new JButton();
clearJButton.setBounds(150, 300, 100, 20);
clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
contentPane.add(clearJButton);
clearJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
clearJButtonActionPerformed(event);
}
}
);
closeJButton = new JButton();
closeJButton.setBounds(275, 300, 100, 20);
closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
closeJButton.setText("Close");
closeJButton.setForeground(black);
closeJButton.setBackground(white);
contentPane.add(closeJButton);
closeJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
closeJButtonActionPerformed(event);
}
}
);
setTitle("Paint Calculator");
setSize(400, 400);
setVisible(true);
}
// main method
public static void main(String[] args)
{
PaintCalculator application = new PaintCalculator();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enterJButtonActionPerformed(ActionEvent event)
{
getRoomWidthInput();
currency = new DecimalFormat("$0.00");
gallonQuantity = calculateGallonQuantity();
gallonQuantityJTextField.setText("" + gallonQuantity);
subTotal = calculateSubTotal();
subTotalJTextField.setText("" + currency.format(subTotal));
salesTax = calculateSalesTax();
salesTaxJTextField.setText("" + currency.format(salesTax));
totalSales = calculateTotalSales();
totalSalesJTextField.setText("" + currency.format(totalSales));
}
public void getRoomWidthInput()
{
try
{
roomWidth = Integer.parseInt(roomWidthJTextField.getText());
getRoomLengthInput();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter width of room!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
roomWidthJTextField.setText("");
roomWidthJTextField.requestFocusInWindow();
}
}
public void getRoomLengthInput()
{
try
{
roomLength = Integer.parseInt(roomLengthJTextField.getText());
calculateTotalSales();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter length of room!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
roomLengthJTextField.setText("");
roomLengthJTextField.requestFocusInWindow();
}
}
public double calculateGallonQuantity()
{
roomArea = roomWidth * roomLength;
gallonQuantity = roomArea / 400;
return gallonQuantity;
}
public double calculateSubTotal()
{
subTotal = gallonQuantity * 24.95;
return subTotal;
}
public double calculateSalesTax()
{
TAX_RATE = .07;
salesTax = subTotal * TAX_RATE;
return salesTax;
}
public double calculateTotalSales()
{
totalSales = subTotal + salesTax;
return totalSales;
}
public void clearJButtonActionPerformed(ActionEvent event)
{
roomWidthJTextField.setText("");
roomWidthJTextField.requestFocusInWindow();
roomLengthJTextField.setText("");
gallonQuantityJTextField.setText("");
subTotalJTextField.setText("");
salesTaxJTextField.setText("");
totalSalesJTextField.setText("");
}
public void closeJButtonActionPerformed(ActionEvent event)
{
System.exit(0);
}
}