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);
	}
}

hi friend
i am also new in java but i will give the solution to you.
your calc work only when the data in enterd in it so why not you dissable the calculate button until the right data will get you can also give tool tip so the user can understand which type of data is enterd e.g give tool tip like Enter the date in format YYYY/MM/DD
like 2010/02/14 ok.
because of this you can reduce the work of user insted of giving message of bad input. its always nice idea do not get bad input but stop the bad input. focus on this. good luck

thank for your idea, but my teacher want it that way. He want if no value is input, then error message display. I can't go any other choice.

This article has been dead for over six months. Start a new discussion instead.