When I put these code in my project. It suppose to display error box when no input is enter, but there no dialog box is pop up. What else do I need for this code to work? Here part of the code I want it to work
if( costKwhrField.getText().length() == 0 )
{
JOptionPane.showMessageDialog( null, "Kilowatt cost required",
"Input Required", JOptionPane.WARNING_MESSAGE );
return;
}
And here the is the full one
/*
Chapter 3: Creating an Applet
Student: An Van Nguyen
Date: February 9, 2010
Filenam: Assignmen5.java
Purpose: Class Assignment
*/
// #3
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
//#4
public class KilowattApplet extends Applet implements ActionListener
{
// #9
double average;
// #5
Label welcome = new Label ("Welcome to the Appliance Energy Calculator");
Label costKwhrLabel = new Label ("Please enter the cost per kilowatt-hour in cents: ");
TextField costKwhrField = new TextField (5);
Label hoursPerYearLabel = new Label ("Please enter the kilowatt-hours consumed: ");
TextField hoursPerYearField = new TextField (5);
Button calcButton = new Button ("Calculate");
Label outputLabel = new Label ("Click the Calculate button to display the average energy cost");
// #6
public void init()
{
add(welcome);
add(costKwhrLabel);
add(costKwhrField);
add(hoursPerYearLabel);
add(hoursPerYearField);
add(calcButton);
// #7
calcButton.addActionListener(this);
add(outputLabel);
}
// create a jframe
JFrame frame = new JFrame("JOptionPane showMessageDialog example");
// #8
public void actionPerformed(ActionEvent e)
{
double costKwhr = Double.parseDouble(costKwhrField.getText());
double kwHours = Double.parseDouble(hoursPerYearField.getText());
////////////////////////////////////////////////////////////////////////////
if( costKwhrField.getText().length() == 0 )
{
JOptionPane.showMessageDialog( null, "Kilowatt cost required",
"Input Required", JOptionPane.WARNING_MESSAGE );
return;
}
/*if( hoursPerYearField.getText().length() == 0 )
{
JOptionPane.showMessageDialog( null, "Kilowatt consumed required",
"Input Required", JOptionPane.WARNING_MESSAGE );
return;
}*/
///////////////////////////////////////////////////////////////////////////////////////
if( costKwhr == 0 )
{
JOptionPane.showMessageDialog( null, "Kilowatt cost cannot be zero",
"Invalid Input", JOptionPane.ERROR_MESSAGE );
return;
}
if( costKwhr == 0 )
{
JOptionPane.showMessageDialog( null, "Kilowatt consumed cannot be zero",
"Invalid Input", JOptionPane.ERROR_MESSAGE );
return;
}
////////////////////////////////////////////////////////////////////////////
// #10
average = costKwhr * kwHours;
// #11
outputLabel.setText("The average annual cost to operate this appliance is $" + Math.round(average*100)/100D);
}
}